使用纯Java JDBC驱动 程序实现数据库的连接出现错误

2024-12-26 05:57:46
推荐回答(5个)
回答1:

lz给你个参考吧
package org.lxy.demo.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataBaseConnection {
private static final String DBDRIVER="org.gjt.mm.mysql.Driver";
private static final String DBURL="jdbc:mysql://localhost:3306/user";
private static final String DBUSER="root";
private static final String DBPASS="mysqladmin";
private Connection con=null;
public DataBaseConnection(){
try {
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
return this.con;
}
public void close(){
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

回答2:

URL (数据库连接字)要写对
Driver 要写对
用户,口令也要对

回答3:

开启sqlserver服务了吗

回答4:

com.microsoft.sqlserver.jdbc.SQLServerDriver
这个是SQL2005的驱动名字

回答5:

看服务里,看下SQL SERVER的实例是不是开启的,开启的哪个实例?