下面这段代码为什么在windows下运行正常,在linux下就报错了?

热血大婶 2015-06-17 01:34:25
以下代码的功能是向数据库里写数据,在windows下可以正常写入,在linux系统中运行就不能,并且报以下错误:
No suitable driver found for jdbc:mysql://109.105.114.137/dingheng
public class FirstJDBC {

Connection con = null;
Statement stmt = null;
String surl, suser, spsw;

FirstJDBC(String url, String user, String psw){
surl = url;
suser=user;
spsw=psw;
connectDB();
}

public void clean(){

try {
con.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public Statement connectDB(){
try {
con = DriverManager.getConnection(surl, suser,spsw);
stmt = con.createStatement();

return stmt;
} catch (SQLException e) {
e.printStackTrace();
}
return null;

}

public void Query(String sql){

ResultSet result = null;
try {
result = stmt.executeQuery(sql);
while(result.next()){
int id = result.getInt("id");
String ban = result.getString("name");
System.out.println(id+" "+ban);
}
if(result!=null){
result.close();
}
} catch (SQLException e) {
e.printStackTrace();
}

}

public void Insert(String sql){
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}

}

public static void main(String[] args) {

FirstJDBC j = new FirstJDBC("jdbc:mysql://109.105.114.137/dingheng", "root", "");

j.Insert("insert into hetest values('linuxx','linuxx')");

j.clean();

}

}
...全文
264 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
injuer 2015-06-18
  • 打赏
  • 举报
回复
把JDBC的jar包放到tomcat的lib下可以解决此问题
skgary 2015-06-18
  • 打赏
  • 举报
回复
估计是你classpath的问题。

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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