【问题标题】:How to connect Database driver to android studio.?如何将数据库驱动程序连接到android studio。?
【发布时间】:2016-07-22 02:20:09
【问题描述】:

据我们所知,Google 已经在新的 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 {

   }
}

【问题讨论】:

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


    【解决方案1】:
    //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;
            }
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 2013-10-25
      • 2018-11-19
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多