[置顶] 求助!!!连接不上本地SQLServer2014数据库,求大神帮忙

时间:2021-11-30 04:38:58

附上源代码:

public class MainActivity extends Activity {


private Button btn;
private TextView showTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn = (Button) findViewById(R.id.btn);
        showTV = (TextView) findViewById(R.id.showTV);
        
        btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String str = linkSQL();
showTV.setText(str);
}
});
    }
    private String linkSQL() {

    String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=my_data";
    String USER = "sa";
    String PWD = "123456";
    String result = null;
   
    try {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PWD);
result = "succeed";
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println(e.toString());
result = e.toString();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.toString());
result = e.toString();
}
    return result;

    
}




这就是源代码,架包添加对了的,直接从数据库管理软件也能连接上数据库,架包下也能找到SQLServerDriver.class这个类,就是一直报这个错误,求各位大神帮帮忙。