Java连接MySql需要下载JDBC驱动MySQL-connector-java-5.0.5.zip。然后将其解压缩到任一目录。解压到D盘,然后将其目录下的MySQL-connector-java-5.0.5-bin.jar加到classpath里,具体如下:
“我的电脑”-> “属性” -> “高级” -> “环境变量”,在系统变量那里编辑classpath,将D:\MySQL-connector-java-5.0.5\MySQL-connector-java-5.0.5-bin.jar加到最后,在加这个字符串前要加“;”,以与前一个classpath区分开,然后确定。
package hqs;
import java.sql.*;
public class DataBasePractice {
public static void main(String[] args) {
//声明Connection对象
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/mydata";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "root";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.创建statement类对象,用来执行SQL语句!!
Statement statement = con.createStatement();
//要执行的SQL语句
String sql = "select * from student";
//3.ResultSet类,用来存放获取的结果集!!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" 学号" + "\t" + " 姓名");
System.out.println("-----------------");
String name = null;
String id = null;
while(rs.next()){
//获取stuname这列数据
name = rs.getString("stuname");
//获取stuid这列数据
id = rs.getString("stuid");
//首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。
//然后使用GB2312字符集解码指定的字节数组。
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
//输出结果
System.out.println(id + "\t" + name);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
}
}
给你一段参考代码:
package 数据库编程;
import java.sql.*;
public class 数据库连接 {
public static void main(String[] args) throws SQLException{
//1.加载驱动程序
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.创建与DB数据库的连接
//连接字符串
String url="jdbc:mysql://192.168.1.101:3306/java数据库?user=root&password=humin";
Connection con=DriverManager.getConnection(url);
//进行读写
if(!con.isClosed()){
System.out.print("欢迎访问我的数据库!\n你想做什么啊………………\n");
Statement st=con.createStatement();
ResultSet rs= st.executeQuery("select * from stu");
while(rs.next()){
System.out.print( rs.getString("stuid")+","+rs.getString("name")+","+rs.getString("sex")+","+rs.getString("age")+","+rs.getString("address")+","+rs.getString("tel")+"\n");
}
}
//关闭数据库
con.close();
}
}
先导入mysql数据库的包!
然后给你一段代码 自己看看
public class DBUtil {
Connection conn;
// 默认构造器,做驱动加载
public DBUtil() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/vote", "root", "111");
} catch (Exception e) {
e.printStackTrace();
}
}
public Connection getConnection() {
try {
if (conn == null || conn.isClosed()) {
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/vote", "root", "111");
}
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public void close() {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}