今天遇到个很奇怪的问题,项目登录上去之后,去修改某张表的一条记录,突然报错:No suitable driver found for jdbc:mysql://127.0.0.1:3306/bank。项目可以登录,那就不是数据库配置文件的问题。其他表的添加修改也可以,那说明数据库的连接也没问题。真搞不懂怎么回报这个错。请大神们指点一二。我感激不仅!
数据库配置文件:
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://127.0.0.1\:3306/bank
#url=jdbc:mysql://127.0.0.1:3306/jdbcstudy
userName=root
password=1234
数据库连接类:
public class JdbcUtils implements Serializable {
private static String driver;
private static String url;
private static String userName;
private static String password;
private static JdbcUtils jdbcUtil=null;
/**
* 单例模式获取实例
*/
private JdbcUtils(){
//读取db.properties文件中的数据库连接信息
InputStream in=JdbcUtils.class.getClassLoader().getResourceAsStream("DB.properties");
Properties prop=new Properties();
try {
prop.load(in);
//获取数据库连接驱动
driver=prop.getProperty("driver");
//获取数据库连接URL地址
url=prop.getProperty("url");
//获取数据库连接用户名
userName=prop.getProperty("userName");
//获取数据库连接密码
password=prop.getProperty("password");
//加载数据库驱动
Class.forName(driver);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static JdbcUtils createInstance(){
if(jdbcUtil==null){
jdbcUtil=new JdbcUtils();
}
return jdbcUtil;
}
/**
* 获取数据库的连接对象
* @return
*/
public static Connection getCon() throws SQLException{
return DriverManager.getConnection(url, userName, password);
}
/**
* 释放资源
* 要释放的资源包括Connection数据库连接对象,负责执行SQL命令的Statement对象,存储查询结果的ResultSet对象
* @param con
* @param st
* @param rs
*/
public static void release(Connection con,Statement st,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
操作类的方法:
public WorkFlowTable getWorkFlowTable(int id) {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
WorkFlowTable wfTable=null;
try {
con=JdbcUtils.createInstance().getCon();//报错点
StringBuffer sql=new StringBuffer("select wt.id wtid,wt.thetable wttable,wt.tablename wttablename,work_id,wt.description wtdes,wf.id wfid ,wf.name wfname, wf.workflowprocess wfprocess ,wf.notes wfnotes,wf.flag wfflag from tb_workflow_table wt left join tb_workflow wf on wt.work_id=wf.id where wt.id=? ");
ps=con.prepareStatement(sql.toString());
ps.setInt(1, id);
rs=ps.executeQuery();
while (rs.next()) {
wfTable=new WorkFlowTable();
wfTable.setId(rs.getInt("wtid"));
wfTable.setThetable(rs.getString("wttable"));
wfTable.setTableName(rs.getString("wttablename"));
wfTable.setDescription(rs.getString("wtdes"));
WorkFlow wf=new WorkFlow();
wf.setId(rs.getInt("wfid"));
wf.setName(rs.getString("wfname"));
wf.setWorkingProcess(rs.getString("wfprocess"));
wf.setNotes(rs.getString("wfnotes"));
wf.setFlag(rs.getString("wfflag"));
wfTable.setWf(wf);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JdbcUtils.release(con, ps, rs);
}
return wfTable;
}