2016-04-02 3 views
-1

Da wir wissen, dass Google einige alte Funktionen für Datenbankimplementierungen im neuen Android SDK veraltet hat, wurden auch neue Funktionen hinzugefügt. Also möchte ich eine kleine App entwickeln, um den Inhalt der Datenbank mit Servlet anzuzeigen. In Android Studio, ich habe auf AppEngine Servlet-Modul erstellt, über die ich auf meine Datenbank in PhpMyAdmin zugreifen möchte. Ich kann hier keine Verbindung zu dieser Datenbank herstellen. Nun, indem ich den folgenden Code verwende, drucke ich die Ausgabe im Browserfenster. Also weiß jemand, wie man Verbindung mit der Datenbank in dieser neuen Funktion bekommt.Wie wird der Datenbanktreiber mit Android Studio verbunden?

package com.example.Nirmal.myapplication.backend; 

import java.io.IOException; 
import javax.servlet.http.*; 
import java.sql.*; 
import java.io.PrintWriter; 

public class MyServlet extends HttpServlet { 
@Override 
public void doGet(HttpServletRequest req, HttpServletResponse resp) 
     throws IOException { 
    PrintWriter out = resp.getWriter(); 
    /*resp.setContentType("text/plain"); 
    resp.getWriter().println("Please use the form to POST to this url");*/ 
    try { 
     //Class.forName("com.mysql.jdbc.Driver"); 

     Connection con=DriverManager.getConnection(
       "jdbc:mysql://localhost:3306/gaming_hub","root",""); 
     out.println("here"); 
     String sql="SELECT * from games"; 
     PreparedStatement ps=con.prepareStatement(sql); 
     ResultSet rs=ps.executeQuery(); 

     // DataOutputStream dout=new DataOutputStream(); 
     while(rs.next()) 
     { 
      out.println(rs.getString(1)); 
     } 

    }catch (Exception e){ 
     out.println("Exception....."); 

    } 
} 

@Override 
public void doPost(HttpServletRequest req, HttpServletResponse resp) 
     throws IOException { 

    } 
} 

Antwort

0
//I found this connection class on 
//http://www.parallelcodes.com/connect-android-to-ms-sql-database-2/ 

package hitesh.sqlapp; 

import android.os.StrictMode; 
import android.util.Log; 
import java.sql.SQLException; 
import java.sql.Connection; 
import java.sql.DriverManager; 

/** 
* Created by H-PC on 16-Oct-15. 
*/ 
public class ConnectionClass { 
    String ip = "ip address"; 
    String classs = "net.sourceforge.jtds.jdbc.Driver"; 
    String db = "databaseName"; 
    String un = "username"; 
    String password = password"; 


    @SuppressLint("NewApi") 
    public Connection CONN() { 
     Log.d("activity", "Connection, connection"); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     Connection conn = null; 
     String ConnURL = null; 
     try { 

      Class.forName(classs); 
      ConnURL = "jdbc:jtds:sqlserver://" + ip + ";" 
        + "databaseName=" + db + ";user=" + un + ";password=" 
        + password + ";"; 
      conn = DriverManager.getConnection(ConnURL); 
     } catch (SQLException se) { 
      Log.e("ERRO", se.getMessage()); 
     } catch (ClassNotFoundException e) { 
      Log.e("ERRO", e.getMessage()); 
     } catch (Exception e) { 
      Log.e("ERRO", e.getMessage()); 
     } 
     return conn; 
    } 
} 

//and this code to retrieve something 

     @Override 
     protected String doInBackground(String... params) { 
      if(userid.trim().equals("")|| password.trim().equals("")) 
       z = "Please enter User Id and Password"; 
      else 
      { 
       try { 
        Connection con = connectionClass.CONN(); 
        if (con == null) { 
         z = "Error in connection with SQL server"; 
        } else { 
         String query = "select * from Usertbl where UserId='" + userid + "' and password='" + password + "'";    
         Statement stmt = con.createStatement(); 
         ResultSet rs = stmt.executeQuery(query); 
         //int aantal = rs.l 
         z = "start loop "; 
         //************************************* 
         try { 
           while (rs.next()) { 
            z = z + "start loop1 "; 
            String user = rs.getString("user"); 
            z = z + "start println "; 
           } 
         } catch (SQLException e) { 
          z = z + "SQLException "; 
         } finally { 
          if (stmt != null) { stmt.close(); } 
         } 
         //*********************************** 
         if(rs.next()) 
         { 
          z = z + "Login successfull "; 
          isSuccess=true; 
         } 
         else 
         { 
          z = z + "Invalid Credential "; 
          isSuccess = false; 
         } 
        } 
       } 
       catch (Exception ex) 
       { 
        isSuccess = false; 
        z = z + "Exceptions "; 
       } 
      } 
      return z; 
     } 
    } 


} 
Verwandte Themen