I have a database in sql anywhere 9.0 and work through sybase, I have a desktop application in POWER BUILDER that enters products, which I want to show them in an application made in android studio, I just have to show a list of those products in my android project, for which I tried to connect using jdbc. In Power builder, I use the odbc to connect, but I know that android uses jdbc, I would like to connect to that database to get the data, I have tried the following:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConectionURL="jdbc:jtds:odbc:driver={Adaptive Server Anywhere 9.0};ENG=ARCHIVO;UID=" + us + ";PWD="+ pass + ";DBN="+bd + ";LINK=TCPIP(HOST="+ip+":2638)";
con = DriverManager.getConnection(ConectionURL);
the jtds is a library downloaded from the internet that allows connections with sql server, the download and the probe in my application but I do not know if it is the correct way to implement it, can someone help me by telling me how to put the url and the class?
Below I put the complete code of my connection.
package com.example.systemsecpc3.aplicacionsystemsproductos.connections;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Created by Systems Ec pc3 on 16/12/2017.
*/
public class Conexion {
//declaramos las variables de conexion
Connection con = null;
public String ip, bd, us, pass;
@SuppressLint("NewApi")
public Conexion(String usuariobd, String clavebd, String BaseDatos, String ServerIp){
us=usuariobd;
bd=BaseDatos;
pass=clavebd;
ip=ServerIp;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String ConectionURL =null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConectionURL="jdbc:jtds:odbc:driver={Adaptive Server Anywhere 9.0};ENG=ARCHIVO;UID=" + us + ";PWD="+ pass + ";DBN="+bd + ";LINK=TCPIP(HOST="+ip+":2638)";
con = DriverManager.getConnection(ConectionURL);
}catch (SQLException se)
{
Log.e("Error SQL 1 : ", se.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("Error Class 2 : ", e.getMessage());
}
catch (Exception e)
{
Log.e("Error Exception 3 : ", e.getMessage());
}
}
public Connection getConnection(){
return con;
}
public void desconectar(){
con = null;
}
}
y este es el codigo de mi mainactivity
package com.example.systemsecpc3.aplicacionsystemsproductos.activities;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.systemsecpc3.aplicacionsystemsproductos.R;
import com.example.systemsecpc3.aplicacionsystemsproductos.connections.Conexion;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;
public class MainActivity extends AppCompatActivity {
//declaramos elementos del layout
Button probar;
TextView texto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//obtenemos en la variable los elementos del layout
probar = (Button) findViewById(R.id.button);
texto = (TextView) findViewById(R.id.textView);
probar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Conexion cc=new Conexion("dba","sql", "SYSTEMS","192.168.1.126");
Connection cn= cc.getConnection();
Statement st=null;
ResultSet rs;
try {
st=(Statement) cn.createStatement();
rs=st.executeQuery("select max(*) from bo_producto");
if(rs.next())
{
texto.setText(rs.getString("ENCTONTRÓ DATOS"));
}
else
{
texto.setText(rs.getString("NO ENCONTRÓ DATOS"));
}
} catch (SQLException ex) {
Log.e("Error here 1 : ", ex.getMessage());
}
}
});
}
}