mysql里用java连接SQL数据库
源代码:importjava.sql.*;publicclassJdbc{publicstaticvoidmain(String[]args){Connectioncn=...
源代码:
import java.sql.*;
public class Jdbc {
public static void main(String[] args) {
Connection cn=null;
Statement stmt=null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=aa";
String user="syx";
String password="";
cn=DriverManager.getConnection(url,user,password);
stmt=cn.createStatement();
stmt.executeUpdate("insert into dbo.Students(id,stu_number,stu_name,stu_sex,stu_age,stu_class,stu_domitary)values('11','0011','何海涛','男','39','软件5班','5-402')");
}catch(Exception e){
e.printStackTrace();
}
finally{
try{
stmt.close();
cn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
错误提示:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Jdbc.main(Jdbc.java:11)
java.lang.NullPointerException
at Jdbc.main(Jdbc.java:19) 展开
import java.sql.*;
public class Jdbc {
public static void main(String[] args) {
Connection cn=null;
Statement stmt=null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=aa";
String user="syx";
String password="";
cn=DriverManager.getConnection(url,user,password);
stmt=cn.createStatement();
stmt.executeUpdate("insert into dbo.Students(id,stu_number,stu_name,stu_sex,stu_age,stu_class,stu_domitary)values('11','0011','何海涛','男','39','软件5班','5-402')");
}catch(Exception e){
e.printStackTrace();
}
finally{
try{
stmt.close();
cn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
错误提示:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Jdbc.main(Jdbc.java:11)
java.lang.NullPointerException
at Jdbc.main(Jdbc.java:19) 展开
3个回答
展开全部
String user="root";看看 还要引入一个mysql的包,我有没有引入。
追问
试了还是不行,我用的SQLserver 2005 驱动是sqljdbc_1.2,用户名是“syx-PC\syx”,意思是用户名是syx-PC或syx吧?还有就是用Windows身份验证,没要密码,用SQLserver身份验证就要密码,建表是用SQLserver身份验证,要了密码的,但我再用Windows身份验证登陆,也能进去,就没要密码,密码我也忘了,
追答
这样肯定不行呀,你必须用mysql的驱动,明天我发给你!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有空指针异常,看看是否有录入或者接收空值或则哪一个变量没有初始化。at Jdbc.main(Jdbc.java:11)滴1行有问题,检查一下!!
追问
我用的SQLserver 2005 驱动是sqljdbc_1.2,用户名是“syx-PC\syx”,意思是用户名是syx-PC或syx吧?还有就是用Windows身份验证,没要密码,用SQLserver身份验证就要密码,建表是用SQLserver身份验证,要了密码的,但我再用Windows身份验证登陆,也能进去,就没要密码,密码我也忘了,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询