import java.sql.*;public class DBTester1 {
public static void main(String[] args) throws Exception {
Connection con;
Statement stmt;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String dbUrl =
"jdbc:mysql://localhost:3306/STOREDB?useUnicode=true&characterEncoding=GB2312";
String dbUser = "dbuser";
String dbPwd ="1234";
con = java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
stmt= con.createStatement();
stmt.executeUpdate("insert into CUSTOMERS (NAME,AGE,ADDRESS)"
+"VALUES('小王',20,'上海')");
rs = stmt.executeQuery("SELECT ID,NAME,AGE,ADDRESS from CUSTOEMRS");
while(rs.next())
{
long id = rs.getLong(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
System.out.println("id="+id+",name="+name+",age="+age+",address="+address);
}
stmt.executeUpdate("delete from CUSTOMERS where name='小王'");
rs.close();
stmt.close();
con.close();
}
}package com.mysql.jdbc does not exist, DriverManager.registerDriver(new com.mysql.jdbc.Driver());,这个是什么问题啊,我在里面是加了引号的啊,
public static void main(String[] args) throws Exception {
Connection con;
Statement stmt;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String dbUrl =
"jdbc:mysql://localhost:3306/STOREDB?useUnicode=true&characterEncoding=GB2312";
String dbUser = "dbuser";
String dbPwd ="1234";
con = java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
stmt= con.createStatement();
stmt.executeUpdate("insert into CUSTOMERS (NAME,AGE,ADDRESS)"
+"VALUES('小王',20,'上海')");
rs = stmt.executeQuery("SELECT ID,NAME,AGE,ADDRESS from CUSTOEMRS");
while(rs.next())
{
long id = rs.getLong(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
System.out.println("id="+id+",name="+name+",age="+age+",address="+address);
}
stmt.executeUpdate("delete from CUSTOMERS where name='小王'");
rs.close();
stmt.close();
con.close();
}
}package com.mysql.jdbc does not exist, DriverManager.registerDriver(new com.mysql.jdbc.Driver());,这个是什么问题啊,我在里面是加了引号的啊,
解决方案 »
- eclipse中的web工程导入myeclipse中,出现servlet错误,怎么解决?
- 大哥进来瞅瞅吧,我实在是弄不了了
- 急求助~!在线等~!新手~!
- 如何用js在jsp页面中找到相同标签啊?
- 【垂死挣扎】在MyEclipse中如何启动JBoss啊 怎么在控制台上可以 在MyEclipse怎么就不好使呢?
- 怎么判断自动检测网络连接是否正常?
- 现做一模块,实现图片的复制.结果却看不到图象!(来者有分)
- 下载验证码图片至本地
- 关于tomcat--急,在线等,分不够再加
- 怎么让浏览器在用户后退时不自动重现用户以前的选择?
- 100分求助关于JSP页面编码乱码问题
- java web 文件下载 如何实现?
把驱动包引入
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
这里重复,驱动只要注册一次就可以了
放到 jre\lib\ext\
import java.sql.*;public class Conn { private static Connection con;
private Statement statement;
private ResultSet rs;
private static final String userName = "root";
private static final String userPassword = "123456";
private static final String dbName = "hoot";
private static final String encoding = "characterEncoding=gb2312&useUnicode=true";
private static final String driveName = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost/" + dbName + "?" + encoding + "&user=" + userName +
"&password" + "=" + userPassword; public static synchronized Connection getCon() throws Exception {
try {
Class.forName(driveName).newInstance();
con = DriverManager.getConnection(url);
return con;
} catch (SQLException e) {
System.err.println(e.getMessage());
throw e;
}
} public Statement getStmntRead() {
try {
con = getCon();
statement = con.createStatement();
return statement;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
} public ResultSet getRs(String sql) {
try {
statement = getStmntRead();
rs = statement.executeQuery(sql);
return rs;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
} public Statement getStmnt() {
try {
con = getCon();
statement = con.createStatement();
return statement;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
} public synchronized void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
try {
if (statement != null) {
statement.close();
statement = null;
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
try {
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
这个事我之前弄的 你对比着看下吧
查看里面Driver.class有没有在com/mysql/jdbc下面.