yangsj85
bluegloom
2019-03-22 13:24

Java+Android连不上sqlserver2012

5
  • android

package com.example.talent;
import java.sql.*;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import java.lang.*;
import android.R.string;
public class db {
private static String IP = "192.168.2.161";
private static String DBName = "tisman";
private static String USER = "sa";
private static String PWD = "bluegloom";
/** 创建数据库对象 */
private static Connection getSQLConnection() {
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://" + IP + ":1433/" + DBName + ";useunicode=true;characterEncoding=UTF-8", USER, PWD);
} catch (ClassNotFoundException e) {
System.out.print(e.getMessage());
} catch (SQLException e) {
System.out.print(e.getMessage());
}
return con;
}

    /** 查询数据库 */
    public static String Query() {
        String result = "";
        try {
            Connection conn = getSQLConnection();
            String sql = "select top 1 * from Users";
            Statement stmt = conn.createStatement();//
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String s1 = rs.getString("id");
                String s2 = rs.getString("password");
                result += s1 + "  -  " + s2 + "\n";
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
            result += "查询数据异常!" + e.getMessage();
        }
        return result;
    }

}

运行到class.forname处就报异常
配置如图片说明
下:[图片]

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换