带有Service Pack 2的SQL Server Developer Edition的jTDS连接字符串

时间:2015-08-10 10:50:16

标签: java sql-server jdbc connection-string jtds

我正在构建一个Android应用程序。我ConnectionURL的位置

ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                + "databaseName=" + database + ";user=" + user
                + ";password=" + password + ";";

度Acc。这个回答:Help me create a jTDS connection string

connectionString应该是

jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value[;...]]        

jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS;user=sa;password=s3cr3t

或者,如果您更喜欢使用getConnection(url,&#34; sa&#34;,&#34; s3cr3t&#34;):

jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS

但是,我没有使用SQLEXPRESS。我使用的是Microsoft SQL Server Developer Edition。

我所做的是,一键点击(android)事件发生。登录表单验证UserName和Password并写入新的TextEdit。

package com.muchmore.mydatabase;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class MainActivity extends AppCompatActivity {
     Button loginbtn;
     TextView errorlbl;
     EditText edname, edpassword;
     Connection connect;
     PreparedStatement preparedStatement;
     Statement st;
     String ipaddress, db, username, password;

     @SuppressLint("NewApi")
            private Connection ConnectionHelper(String user, String password,
                                    String database, String server) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
            StrictMode.setThreadPolicy(policy);
            Connection connection = null;
            String ConnectionURL = null;
            try {
                   Class.forName("net.sourceforge.jtds.jdbc.Driver");

                  ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                                 + "databaseName=" + database + ";user=" + user
                                 + ";password=" + password + ";";
                  /*
                  I was tyring to built a right connection string here. Please get it working :) 
                  ConnectionURL = "jdbc:jtds:sqlserver://localhost/"
                                 + "databaseName=" + "MyDatabase" + ";user=" + "sa"
                                 + ";password=" + "sa@123" + ";";
                  */
                 //private String URL =            "jdbc:jtds:sqlserver://localhost/brandix;instance=sqlexpress;useNTLMv2=tru‌​e;domain=workgroup"
                 connection = DriverManager.getConnection(ConnectionURL);
                 } 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 connection;
             }

             @Override
             protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);

             loginbtn = (Button) findViewById(R.id.btnlogin);

             errorlbl = (TextView) findViewById(R.id.lblerror);

             edname = (EditText) findViewById(R.id.txtname);
             edpassword = (EditText) findViewById(R.id.txtpassword);

             ipaddress = "127.0.0.1";
             db = "MyDatabase";
             username = "sa";
             password = "sa@123";
             connect = ConnectionHelper(username, password, db, ipaddress);
             loginbtn.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v) {
                 try {
                      connect = ConnectionHelper(username, password, db, ipaddress);

                      st = connect.createStatement();
                      ResultSet rs = st.executeQuery("select * from login where userid='" + edname.getText().toString() + "' and password='" + edpassword.getText().toString() + "'");
                      if (rs != null && rs.next()) {
                                errorlbl.setText("Login Successful!!!");
                       } else {
                                errorlbl.setText("Sorry, wrong credentials!!!");
                      }

                      } catch (SQLException e) {
                           errorlbl.setText(e.getMessage().toString());
                      }
                }
    });

连接引用中有空引用。所以,我确信连接字符串中有一些错误。否则我的android代码看起来很好。请看正确的字符串格式。

1 个答案:

答案 0 :(得分:1)

MS SQL有两种最流行的JDBC驱动程序。一个是JTDS,另一个是官方Microsoft JDBC驱动程序。 看起来您混合了两个驱动程序的连接字符串语法。

JTDS驱动程序连接字符串示例是: jdbc:jtds:sqlserver://NETWORK_HOST_NAME_OR_IP:1433/NAME_OF_DATABASE

有关创建此内容的更多提示可以找到:http://jtds.sourceforge.net/faq.html#urlFormat

Microsoft JDBC连接字符串示例是: jdbc:sqlserver://NETWORK_HOST_NAME_OR_IP:1433;databaseName=NAME_OF_DATABASE

请注意,实例名称是可选的。请查看本文以获取全面的文档:https://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx

如果您需要找出要在连接字符串中使用的MSSQL实例名称,最简单的方法是执行查询:

SELECT @@SERVERNAME