1.创建一个java project,建一个lib文件。
下载MySQL-connecor。
将下载好的文件打开,将mysql-connector-java-5.0.8-bin.jar文件粘贴复制到lib文件下。
2.构件Classpath路径
3.开发一个JDBC程序
3.1在MySQL中 创建一个数据库:
1 create database day06; 2 3 use day06; 4 5 6 7 create table users( 8 9 id int primary key auto_increment, 10 11 name varchar(40), 12 13 password varchar(40), 14 15 email varchar(60), 16 17 birthday date 18 19 )character set utf8 collate utf8_general_ci; 20 21 22 23 insert into users(name,password,email,birthday) values('zs','123456','zs@sina.com','1980-12-04'); 24 25 insert into users(name,password,email,birthday) values('lisi','123456','lisi@sina.com','1981-12-04'); 26 27 insert into users(name,password,email,birthday) values('wangwu','123456','wangwu@sina.com','1979-12-04');
3.2 创建javaproject项目,添加数据驱动
3.3实现JDBC操作
//1、注册驱动
//2、创建连接
//3、得到执行sql语句的Statement对象
//4、执行sql语句,并返回结果
//5、处理结果
//6关闭资源
package bao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class db1 { public static void main(String[] args) throws Exception{ //1.注册驱动 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //不建议使用,用Class.forName("com.mysql.jdbc.Driver"); //2.创建链接 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/day06", "root", "123") ; //DriverManager.getConnection(url, user, password) //3.得到操作数据库sql语句的对象statement Statement stmt = conn.createStatement(); //4.执行sql语句 String sql="select * from users"; ResultSet rs=stmt.executeQuery(sql); //5.如何有返回结果,处理结果 while(rs.next()){ System.out.println(rs.getObject(1)); System.out.println(rs.getObject(2)); System.out.println(rs.getObject(3)); System.out.println(rs.getObject(4)); System.out.println(rs.getObject(5)); System.out.println("--------------------"); } //6.关闭资源 rs.close(); stmt.close(); conn.close(); } }