如何将数据库驱动程序连接到android studio.?

时间:2016-04-02 07:32:26

标签: java android mysql google-app-engine servlets

我们知道谷歌已经弃用了新的Android SDK中数据库实现的一些旧功能,但也添加了新功能。所以我想开发一个小应用程序来使用servlet显示数据库的内容。 在android studio中,我在AppEngine servlet模块上创建,我想通过它来访问我在PhpMyAdmin中构建的数据库。我在这里无法连接到那个数据库。现在,通过使用下面的代码,我在浏览器窗口打印输出。 所以任何人都知道如何在这个新功能中获得与数据库的连接。

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 {

   }
}

1 个答案:

答案 0 :(得分: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;
        }
    }


}