无法直接从Android连接到SQL Server

时间:2014-08-13 09:29:01

标签: android mysql sql-server database

我正在测试应用程序从Android直接连接到SQL Server bue无法直接从Android连接到SQL Server。浏览器服务正在运行,TCP端口已启用,仍然发出奇怪的错误。还包括互联网权限

问题是什么

import java.sql.*;
import android.os.*;
import android.util.*;
import android.app.*;
import net.sourceforge.jtds.jdbc.*;
import android.widget.*;
public class MainActivity extends Activity

{
TextView txt;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView)findViewById(R.id.txt);
getData();
}
public void getData()
{
Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try
{
/*
String dbName = "DatabaseNew";
String driver = "net.sourceforge.jtds.jdbc.Driver";
String connString = "jdbc:jtds:sqlserver://KashifPC:1433/DatabaseNew;instance=MSSQLSERVER;user=gulzar;password=allah";
//String userName = "gulzar";
//String password = "allah";
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(connString,userName,password);
*/
String connString="jdbc:sqlserver://127.0.0.1:1433;databaseName=DatabaseNew;";
Class.forName("com.microsoft.sqlserver.jdbc.DriverJDBCVersion").newInstance();
conn = DriverManager.getConnection(connString,"gulzar","allah");
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("Select * from dbo.StuAndroid");
while(reset.next())
{
Log.w("Data:",reset.getString(1));
Log.w("Data:",reset.getString(2));
Log.w("Data:",reset.getString(3));
txt.setText(reset.getString(2)+"\n");
}
stmt.close();
conn.close();
} catch (Exception e)
{
Log.w("Error connection","Error :" + e.getMessage());
}

}
}

2 个答案:

答案 0 :(得分:2)

127.0.0.1是您的私人IP地址......您应该使用服务器的公共IP地址 -

转到服务器主机并从here

获取公共IP

答案 1 :(得分:1)

使用服务器的公共IP地址试用此代码。

        String email_id = request.getParameter("EMAILID");
        String user_id = request.getParameter("USERID");

        //String searchQuery = "select * from [USER]";
        String searchQuery = "select [STATE],COUNTRY  from [USER] where EMAILID='"
                + email_id + "' AND USERID ='" + user_id + "'";

        try {
            // Class.forName("com.mysql.jdbc.Driver");
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }
        try {


            Connection con = DriverManager
                    .getConnection(
                            "jdbc:sqlserver://your_public_ip; databaseName=TEST",
                            "user_name", "pwd");


            PreparedStatement stmt = con.prepareStatement(searchQuery);

            ResultSet rs = stmt.executeQuery();


            boolean isEmpty = rs.next();
            if (!isEmpty) {
                // redirect to error page
                /*System.out.println("Username Data is"
                        + rs.getString("USERNAME"));*/
                response.sendRedirect("LoginFailure.jsp");
            } else if (isEmpty) {
                // fetch the session from request, create new session if session
                // is not present in the request
                // System.out.println("Username Data is"+rs.getString("USERNAME"));
                HttpSession session = request.getSession(true);
                session.setAttribute("STATE", rs.getString("STATE"));
                session.setAttribute("COUNTRY", rs.getString("COUNTRY"));

                // redirect to success page
                response.sendRedirect("LoginSuccess.jsp");
            }
        } catch (SQLException e) {
            System.out.println("SQLException occured: " + e.getMessage());
            e.printStackTrace();
        }

希望它能帮到你很多。