菜鸟求助,这是代码:
import java.sql.*;
public class DBConnection{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
public DBConnection(){
try{
//加载驱动
Class.forName("com.microsoft.sql.sqlserver.SQLServerDriver");
String sql="select *from stu";
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/Student","root","123");//连接数据库
stmt=conn.createStatement();//创建Statement对象
//rs=stmt.executeQuery("select*from stu");
}catch(Exception ex){
ex.printStackTrace();
}
}
ResultSet executeQuery(String sql){
//执行查询调用executeQuert 方法
try {
rs=stmt.executeQuery(sql);
}catch(Exception e){
}
return rs;
}
void executeUpdate(String sql){//执行其他操作调用executeUpdate 方法
try{
stmt.executeUpdate(sql);
}
catch(SQLException e){}
}
void close(){//关闭
try{
stmt.close();
conn.close();
}catch(SQLException e){}
}
}运行之后结果是:there are no items to show。
这是怎么回事 ?(我已把mysql-connector-java-5.1.12-bin.jar拷到C:\j2sdk1.4.2_08\jre\lib\ext中)
import java.sql.*;
public class DBConnection{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
public DBConnection(){
try{
//加载驱动
Class.forName("com.microsoft.sql.sqlserver.SQLServerDriver");
String sql="select *from stu";
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/Student","root","123");//连接数据库
stmt=conn.createStatement();//创建Statement对象
//rs=stmt.executeQuery("select*from stu");
}catch(Exception ex){
ex.printStackTrace();
}
}
ResultSet executeQuery(String sql){
//执行查询调用executeQuert 方法
try {
rs=stmt.executeQuery(sql);
}catch(Exception e){
}
return rs;
}
void executeUpdate(String sql){//执行其他操作调用executeUpdate 方法
try{
stmt.executeUpdate(sql);
}
catch(SQLException e){}
}
void close(){//关闭
try{
stmt.close();
conn.close();
}catch(SQLException e){}
}
}运行之后结果是:there are no items to show。
这是怎么回事 ?(我已把mysql-connector-java-5.1.12-bin.jar拷到C:\j2sdk1.4.2_08\jre\lib\ext中)
解决方案 »
- 一个关于连接数据库的问题 ClassNotFoundException
- 关于数据库查询语句的疑惑,问题怎么解决?
- 关于ehcache缓存的使用问题,有点不解。第一次用缓存框架
- 刚上路,求救hibernate 的小问题???
- 请各位大哥帮帮小弟忙,关于根据分隔符取值问题(特急)
- 在jbuilderx下面怎样配制 tomcat4.1的数据库连接池
- 又是jb9+axis,用自动生成的客户端测试的时候报这个错!!
- 请高手指点一下
- jbuilder 的注册问题
- 如何用SAX从XML文件中截取一段内容并将该内容生成为另一个XML文件?
- 用Struts2标签创建表单的问题
- 操作文件的一个问题,我怎么在一个文件只用检索出来相应的东西然后删除?
将mysql-connector-java-5.1.12-bin.jar拷到工程下的WEBAPP/WEB/WEB-INF/LIB下。
将mysql-connector-java-5.1.12-bin.jar拷到工程下的WEBROOT/WEB-INF/LIB下。
你连MYSQL,写的却是MSSQL,这当然不可以了,在项目中要把JAR放在你的classpath下
import java.sql.*;
public class mysql {
public static void main(String[] args){
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/medicine";
String user = "root";
String password = "hello";
String test = "http://health.sohu.com/20090828/n266291535.shtml";try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select * from news order by time desc";
/*
String value = "搜狐";
String sql_insert = "insert into news(sourse,href,title,time) values ('"+value+"','http://www.sohu.com.cn','医疗','2010')";
try{
int result = statement.executeUpdate(sql_insert);
}catch(SQLException ex){
ex.printStackTrace();
}
*/
ResultSet rs = statement.executeQuery(sql);
String name = null;
while(rs.next())
{
name = rs.getString("sourse");
System.out.println(rs.getString("id")+"\t"+rs.getString("href") + "\t" + name + "\t" + rs.getString("title")+"\t"+rs.getString("time"));
} rs.close();
conn.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace(); } catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace(); }
System.out.println("-----------------------------------------");
}}
/*Class.forName("com.microsoft.sql.sqlserver.SQLServerDriver");
String sql="select *from stu";
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/Student","root","123");//连接数据库*/
Class.forName("com.mysql.jdbc.Driver");
String sql="select *from stu";
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_dev","root","root");
这是连接SQLserver数据库的啊·
com.mysql.jdbc.Driver注意也要看你自己的mysql包 不一定是这个
在myEclipse找到你的mysql驱动包 看能找到不
如果找到再在这个驱动包里去找Driver类 看跟com.mysql.jdbc的路径是否对应
没找到就是包没导进来
Class.forName("com.microsoft.sql.sqlserver.SQLServerDriver");
驱动写错啦
改为:
Class.forName("com.mysql.jdbc.Driver");
应该就可以了1
改成
com.sql.sqlserver.SQLServerDriver"