java问题Can't Find Database Driver.

kumu789654 2011-11-11 01:20:58
源代码
import java.sql.*;
import java.util.*;
import javax.swing.JOptionPane;
public class SqlManager {
private static SqlManager p=null;
private PropertyResourceBundle bundle;
private static String jdbcDriver=null;
private static String split=null;
private String DBType=null;
private String DBhost="localhost";
private String DBname="";
private String DBport="";
private String DBuser="";
private String DBpassword="";
private Connection Sqlconn=null;
private Statement Sqlstmt=null;
private String strCon=null;
private SqlManager(){
try{
bundle=new PropertyResourceBundle(SqlManager.class.
getResourceAsStream("/sysConfig.properties"));
this.DBhost=getString("DBhost");
this.DBname=getString("DBname");
this.DBport=getString("DBport");
this.DBuser=getString("DBuser");
this.DBpassword=getString("DBpassword");
String system_type=getString("system-type");
if(system_type!=null){
if(system_type!=null){
if(system_type.toLowerCase().equals("widows"))
split=";";
else if(system_type.toLowerCase().equals("unix"))
split=":";
}
String database_type=getString("database-type");
this.DBType=database_type;
if(database_type!=null){
if(database_type.toLowerCase().equals("mysql")){
jdbcDriver="com.mysql.jdbc.Driver";
strCon="jdbc:mysql://"+DBhost+":"+DBport+"/"+DBname;
}
else if(database_type.toLowerCase().equals("oracle")){
jdbcDriver="oracle.jdbc.driver.OracleDriver";
strCon="jdbc:oracle:thin:@"+DBhost+":"+DBport+":"+DBname;
}
else if(database_type.toLowerCase().equals("sqlserver")){
jdbcDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
strCon="jdbc:microsoft:sqlserver://"+DBhost+":"+DBport+";DatabaseName="+DBname;
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static SqlManager createInstance(){
if(p==null)
{
p=new SqlManager();
p.initDB();
}
return p;
}
private String getString(String s)
{
return this.bundle.getString(s);
}

public void initDB(){
System.out.println(strCon);
System.out.println(jdbcDriver);
try{
Class.forName(jdbcDriver);
}catch(Exception ex){
System.err.println("Can't Find Database Driver.");
}
}
public void connectDB(){
try{
System.out.println("SqlManager:Connecting to database...");
Sqlconn=DriverManager.getConnection(strCon,DBuser,DBpassword);
Sqlstmt=Sqlconn.createStatement();
}catch(SQLException ex){
System.err.println("connectDB"+ex.getMessage());
}
System.out.println("SqlManager:Connect to database successful.");
}
public void closeDB(){
try{
System.out.println("SqlManager:Close connection to database...");
Sqlstmt.close();
Sqlconn.close();
}catch(SQLException ex){
System.err.println("closeDB:"+ex.getMessage());
}
System.out.println("Sqlmanager:Close connection successful.");
}
public int executeUpdate(String sql){
int ret=0;
try{
ret=Sqlstmt.executeUpdate(sql);
}catch(SQLException ex)
{
System.out.println("executeUpdate:"+ex.getMessage());
}
return ret;
}
public ResultSet executeQuery(String sql){
ResultSet rs=null;
try{
rs=Sqlstmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println("executeQuery:"+ex.getMessage());
}
return rs;
}
}
我是新手,请详细给我解答一下,谢谢!
...全文
418 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq931226110 2011-11-11
  • 打赏
  • 举报
回复
找不到数据库驱动,加下jar包
LPZLSDJF 2011-11-11
  • 打赏
  • 举报
回复
没有数据库驱动JAR包,加一下就OK了
AoYuanSE 2011-11-11
  • 打赏
  • 举报
回复
导入jar包了吗?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧