日期:2014-05-20  浏览次数:20708 次

java连接本地数据库的问题,高手帮忙!!
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;//为什么无法解析
import com.mysql.jdbc.Statement;//为什么无法解析
public class conn extends Thread{ //创建数据库连接类
public static void main(String[] args) { //主方法
String dbUrl="jdbc:mysql://localhost:3306/student"; //定义连接数据库的url。
String dbUser="root"; //定义登录数据库的用户名
String dbPwd="123456"; //定义登录数据库的密码
Connection conn=null; //创建Connection类的对象conn,并让指向空
Connection stmt=null; //创建Statement类的对象stmt,并让指向空

try{
Class.forName("com.mysql.jdbc.Driver");//加载驱动
}catch(ClassNotFoundException e){
e.printStackTrace();
}
String Str="creat table course"+"(id INTEGER,cno VARCHAR(32),cname FLOAT)";
try{
conn=(Connection)DriverManager.getConnection(dbUrl,dbUser,dbPwd);
System.out.println("获得和MySQL数据库的一个连接!");
stmt=(java.sql.Connection)conn.createStatement();
System.out.println("创建一个语句对象!");
((java.sql.Statement) stmt).executeUpdate(Str);
System.out.println("执行创建表的SQL语句!");
System.out.print("在student数据库中创建了表course!");
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}


这个程序为什么运行不了,无法连接到本地数据库啊!!!求高手指点!!

------解决方案--------------------
mysql的jdbc驱动包没加载进来?
------解决方案--------------------
是java.sql.SQLException;
import java.sql.Connection;//为什么无法解析
import java.sql.Statement;//为什么无法解析

------解决方案--------------------
下载:http://ishare.iask.sina.com.cn/f/13760780.html


右击工程选择properties->Java Build Path->Libraries->Add External JARs。
选择下载的那个jar文件
------解决方案--------------------
没加Jar包
------解决方案--------------------
JAVA研究的真小呀,
我现在在努力学习C#中
------解决方案--------------------
看你这情况应该是没有加载jdbc驱动包