关于MySQL数据库与java的连接问题
MySQL安装的是5.6.1社区版,安装时一起装了Connector J;jdk是1.7.0_25,建立了个数据库school,表student,代码如下:
package mypack;
import java.sql.*;
public class Sql1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/school";
String user="root";
String password="root123";
try{
Class.forName(driver);
Connection conn=DriverManager.getConnection(url,user,password);
if(!conn.isClosed())
System.out.println("Succeed connecting to the Database!");
Statement st=conn.createStatement();
String sql="select * from student";
ResultSet rs=st.executeQuery(sql);
System.out.println("---------------------");
System.out.println("执行结果如下所示:");
System.out.println("---------------------");
System.out.println("学号"+"\t"+"姓名"+"年龄");
String name=null;
while(rs.next()){
name=rs.getString("sname");
name=new String(name.getBytes("ISO-8859-1"),"GB2312");
System.out.println(rs.getString("sid")+"\t"+name+"\t"
+rs.getString("sage"));
}
rs.close();
conn.close();
}catch(ClassNotFoundException s)
{
System.out.println("Sorry , can't find the Driver!");
s.printStackTrace();
}catch(SQLException e)
{
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
运行后结果如下:
Sorry , can't find the Driver!
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at mypack.Sql1.main(Sql1.java:12)
由于新手找了好长时间也不知道问题在呢,拜托各位大神帮我看看? 数据库 mysql java --------------------编程问答-------------------- 需要mysql驱动包,mysql-connector-java-5.0.8-bin.jar --------------------编程问答-------------------- e 有架包么? --------------------编程问答-------------------- 亲,你导入mysql的驱动jar包了没?导入一个mysql的jar包 用的jdbc连接的话,再导一个jdbc的驱动jar包,不知道怎么上传附件,楼主自己百度吧。 --------------------编程问答-------------------- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
这里不就说了,没有发现驱动包 --------------------编程问答-------------------- 非常感谢各位!问题解决了!
补充:Java , Java SE