一、建表:
二、获取数据库的连接驱动:
1:导入mysql的驱动jar包,mysql-connector-java-5.1.8-bin.jar
2:写代码连接数据库,如下:
package com.hlcui.file; 5 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.sql.SQLException; 9 10 /** 11 * @author Administrator 12 * 13 */ 14 public class DBUtil { 15 // 定义数据库连接参数 16 public static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; 17 public static final String URL = "jdbc:mysql://localhost:3306/test"; 18 public static final String USERNAME = "root"; 19 public static final String PASSWORD = "root"; 20 21 // 注册数据库驱动 22 static { 23 try { 24 Class.forName(DRIVER_CLASS_NAME); 25 } catch (ClassNotFoundException e) { 26 System.out.println("注册失败!"); 27 e.printStackTrace(); 28 } 29 } 30 31 // 获取连接 32 public static Connection getConn() throws SQLExcep