最近在搞java,想连个SqlServer连不上,可以加载驱动,但是连不上数据库。
数据库版本为SqlServer2017
代码如下:
package util;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Date;
public class DBManager {
static int num;
static Statement st=null;
static ResultSet rs=null;
static Connection con=null;
static String url="jdbc:sqlserver://LAPTOP-LG07MEUA:1433;DataBaseName=Java_Data";
public static void exec(String s) throws SQLException
{
String user,passwd;
user="sa";
passwd="1234567";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("数据库驱动加载成功。");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//就是这里,无法链接数据库
con=DriverManager.getConnection(url, user, passwd);
System.out.println("10");
st=con.createStatement();
System.out.println("1");
try
{
if(s.startsWith("select")||s.startsWith("SELECT"))
rs=st.executeQuery(s);
if(s.startsWith("delete")||s.startsWith("DELETE"))
num=st.executeUpdate(s);
if(s.startsWith("update")||s.startsWith("UPDATE"))
num=st.executeUpdate(s);
if(s.startsWith("insert")||s.startsWith("INSERT"))
num=st.executeUpdate(s);
System.out.println("修改成功!");
}
catch(Exception e)
{
System.out.println("进行数据库修改时失败。");
}
finally
{
try
{
if(rs!=null)
rs.close();
if(st!=null)
st.close();
if(con!=null)
con.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws SQLException {
int i=0;
int[] Resultage=new int [20];
String[] Resultxh=new String [20];
String[] Resultxm=new String [20];
String[] Resultxb=new String [20];
String[] Resultzy=new String [20];
Date[] Resultdate=new Date [20];
String s="SELECT * FROM xsb";
DBManager.exec(s);
System.out.println("学号"+""+"姓名"+" "+"性别"+" "+"出生日期"+" "+"专业"+" "+"年龄");
while(rs.next())
{
Resultxh[i]=rs.getString("学号");
Resultxm[i]=rs.getString("姓名");
Resultxb[i]=rs.getString("性别");
Resultdate[i]=rs.getDate("出生日期");
Resultzy[i]=rs.getString("专业");
Resultage[i]=rs.getInt("年龄");
System.out.print(Resultxh[i]+" ");
System.out.print(Resultxm[i]+" ");
System.out.print(Resultxb[i]+" ");
System.out.print(Resultdate[i]+" ");
System.out.print(Resultzy[i]+" ");
System.out.println(Resultage[i]);
}
}
}
报错为:
数据库驱动加载成功。
Exception in thread "main" java.lang.NullPointerException
at javax.xml.bind.DatatypeConverter.parseHexBinary(DatatypeConverter.java:310)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4098)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3160)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3123)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1981)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at util.DBManager.exec(DBManager.java:27)
at util.DBManager.main(DBManager.java:72)
求大佬帮忙解答。