Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.systemsecpc3.aplicacionsystemsproductos.connections;
- //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;
- }
- package com.example.systemsecpc3.aplicacionsystemsproductos.activities;
- //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());
- }
- }
- });
- }
Add Comment
Please, Sign In to add comment