Guest User

Untitled

a guest
Dec 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. package com.example.systemsecpc3.aplicacionsystemsproductos.connections;
  2.  
  3. //declaramos las variables de conexion
  4. Connection con = null;
  5. public String ip, bd, us, pass;
  6.  
  7. @SuppressLint("NewApi")
  8. public Conexion(String usuariobd, String clavebd, String BaseDatos, String ServerIp){
  9. us=usuariobd;
  10. bd=BaseDatos;
  11. pass=clavebd;
  12. ip=ServerIp;
  13.  
  14. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  15. StrictMode.setThreadPolicy(policy);
  16. String ConectionURL =null;
  17.  
  18. try {
  19. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  20. ConectionURL="jdbc:jtds:odbc:driver={Adaptive Server Anywhere 9.0};ENG=ARCHIVO;UID=" + us + ";PWD="+ pass + ";DBN="+bd + ";LINK=TCPIP(HOST="+ip+":2638)";
  21. con = DriverManager.getConnection(ConectionURL);
  22.  
  23. }catch (SQLException se)
  24. {
  25. Log.e("Error SQL 1 : ", se.getMessage());
  26. }
  27. catch (ClassNotFoundException e)
  28. {
  29. Log.e("Error Class 2 : ", e.getMessage());
  30. }
  31. catch (Exception e)
  32. {
  33. Log.e("Error Exception 3 : ", e.getMessage());
  34. }
  35.  
  36. }
  37.  
  38.  
  39. public Connection getConnection(){
  40. return con;
  41. }
  42. public void desconectar(){
  43. con = null;
  44. }
  45.  
  46. package com.example.systemsecpc3.aplicacionsystemsproductos.activities;
  47.  
  48. //declaramos elementos del layout
  49. Button probar;
  50. TextView texto;
  51.  
  52. @Override
  53. protected void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. setContentView(R.layout.activity_main);
  56.  
  57. //obtenemos en la variable los elementos del layout
  58. probar = (Button) findViewById(R.id.button);
  59. texto = (TextView) findViewById(R.id.textView);
  60.  
  61. probar.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View view) {
  64. Conexion cc=new Conexion("dba","sql", "SYSTEMS","192.168.1.126");
  65. Connection cn= cc.getConnection();
  66. Statement st=null;
  67. ResultSet rs;
  68. try {
  69. st=(Statement) cn.createStatement();
  70. rs=st.executeQuery("select max(*) from bo_producto");
  71. if(rs.next())
  72. {
  73. texto.setText(rs.getString("ENCTONTRÓ DATOS"));
  74. }
  75. else
  76. {
  77. texto.setText(rs.getString("NO ENCONTRÓ DATOS"));
  78. }
  79. } catch (SQLException ex) {
  80. Log.e("Error here 1 : ", ex.getMessage());
  81. }
  82. }
  83. });
  84.  
  85. }
Add Comment
Please, Sign In to add comment