java连接sql数据库
我照着课本编的以下内容:importjava.sql.*;publicclassDateBaseOperation{privateConnectionConnection...
我照着课本编的以下内容:
import java.sql.*;
public class DateBaseOperation
{
private Connection Connection1;
public DateBaseOperation(String driver,String url,String user,String password)throws ClassNotFoundException,SQLException
{
this.Connection1=null;
Class .forName(driver);
this.Connection1=DriverManager.getConnection(url,user,password);
}
public DateBaseOperation(String driver,String url)throws ClassNotFoundException,SQLException
{
this.Connection1=null;
Class.forName(driver);
this.Connection1=DriverManager.getConnection(url);
}
public void finalize()throws SQLException
{
this.Connection1.close();
}
public String getDBAbout()throws SQLException
{
String message="";
DatabaseMetaData dbmd=this.Connection1.getMetaData();
message="JDBC驱动程序"+dbmd.getDriverName()+" "+dbmd.getDriverVersion()+"\r\n"+
"JDBC URL:"+dbmd.getURL()+"\r\n"+"数据库:"+dbmd.getDatabaseProductName()+"\r\n"+
"数据库版本:"+dbmd.getDatabaseProductVersion()+"\r\n"+
"用户名:"+dbmd.getUserName()+"\r\n";
return message;
}
}
class GetDBMessage
{
public static void main (String[] args)
{
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatebaseName=student";
String user="sa";
String password="sa";
try
{
DateBaseOperation dboper1=new DateBaseOperation(driver,url,user,password);
System.out.println(dboper1.getDBAbout());
}
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:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at DateBaseOperation.<init>(DateBaseOperation.java:10)
at GetDBMessage.main(DateBaseOperation.java:44)
Process completed. 展开
import java.sql.*;
public class DateBaseOperation
{
private Connection Connection1;
public DateBaseOperation(String driver,String url,String user,String password)throws ClassNotFoundException,SQLException
{
this.Connection1=null;
Class .forName(driver);
this.Connection1=DriverManager.getConnection(url,user,password);
}
public DateBaseOperation(String driver,String url)throws ClassNotFoundException,SQLException
{
this.Connection1=null;
Class.forName(driver);
this.Connection1=DriverManager.getConnection(url);
}
public void finalize()throws SQLException
{
this.Connection1.close();
}
public String getDBAbout()throws SQLException
{
String message="";
DatabaseMetaData dbmd=this.Connection1.getMetaData();
message="JDBC驱动程序"+dbmd.getDriverName()+" "+dbmd.getDriverVersion()+"\r\n"+
"JDBC URL:"+dbmd.getURL()+"\r\n"+"数据库:"+dbmd.getDatabaseProductName()+"\r\n"+
"数据库版本:"+dbmd.getDatabaseProductVersion()+"\r\n"+
"用户名:"+dbmd.getUserName()+"\r\n";
return message;
}
}
class GetDBMessage
{
public static void main (String[] args)
{
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatebaseName=student";
String user="sa";
String password="sa";
try
{
DateBaseOperation dboper1=new DateBaseOperation(driver,url,user,password);
System.out.println(dboper1.getDBAbout());
}
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:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at DateBaseOperation.<init>(DateBaseOperation.java:10)
at GetDBMessage.main(DateBaseOperation.java:44)
Process completed. 展开
4个回答
展开全部
我不知道你用的是什么数据库
如果是用的MySql的话,我想应该是数据库的连接驱动你没有加载到你的工程里面,才导致了现在的问题。
如果是用的MySql的话,我想应该是数据库的连接驱动你没有加载到你的工程里面,才导致了现在的问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的数据库没有连接上 请检验你传的参数是否正确 就是String driver,String url,String user,String password 这些参数 其中的密码和账号要跟你进入查询分析器输入的一样
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
驱动加载不对
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询