简体   繁体   中英

How to connect Database driver to android studio.?

As we know that Google has deprecated some old features for database implementations in new android SDK, also added new features. So I want develop one small app to display content of database using servlet. In android studio, I've created on AppEngine servlet module through which I want to access my database built in PhpMyAdmin. I'm not able to get connection to that databse here.Right Now, by using below code, I'm printing output on browser window. So Anyone knows how to get connection to the database in this new feature.?

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 {

   }
}
//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;
        }
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM