mysql的驱动包的下载地址:
http://dev.mysql.com/downloads/connector/j/
jar下载后添加到项目
创建数据库
-- 创建数据库
create database test;
use test;
-- 创建表人员信息表
create table userInfo(
id int primary key auto_increment COMMENT '编号',
name varchar(20) not null COMMENT '姓名',
birthday date not null COMMENT '出生年月',
height int not null COMMENT '身高,单位厘米cm',
width int NOT NULL COMMENT '体重,单位千克kg'
);
-- 插入数据
insert into userInfo(name,birthday,height,width)
values
('luolei', '1997-01-24', 179, 60),
('luo', '1997-12-25', 170, 65),
('lei', '1997-03-24', 189, 70)
;
package com.test.jdbc;
import java.util.Date;
/**
*
* @author LUOLEI
* userInfo访问人员信息表的类
* 编号,姓名,出生年月,身高和体重
*/
public class UserInfo {
int id;
String name;
Date birthday;
int height;
int width;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", name='" + name + '\'' +
", birthday=" + birthday +
", height=" + height +
", width=" + width +
'}';
}
}
数据库连接
package com.test.jdbc;
import java.sql.*;
/**
* @author LUOLEI
*/
public class JDBC {
//数据库url、用户名和密码
//解决:The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
//加参数&serverTimezone=UTC
static final String DB_URL="jdbc:mysql://localhost:3306/test?character=utf8&serverTimezone=UTC";
static final String USER="root";
static final String PASS="root";
public static void main(String[] args) {
try {
//1、注册JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取数据库连接
Connection connection = DriverManager.getConnection(DB_URL, USER, PASS);
//3、操作数据库
Statement statement = connection.createStatement();//获取操作数据库的对象
//sql语句
String sql="select * from userInfo";
ResultSet resultSet = statement.executeQuery(sql);//执行sql,获取结果集
while(resultSet.next()){ //遍历结果集,取出数据
UserInfo user = new UserInfo();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setBirthday(resultSet.getDate("birthday"));
user.setHeight(resultSet.getInt("height"));
user.setWidth(resultSet.getInt("width"));
//输出数据
System.out.println(user.toString());
}
//4、关闭结果集、数据库操作对象、数据库连接
resultSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}
}
加载数据库驱动的时候
Class.forName的一个很常见的用法是在加载数据库驱动的时候。
如:
复制收展JavaClass.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:3301;DatabaseName==lx","lx","lx");
- 1
- 2
为什么在我们加载数据库驱动包的时候有的却没有调用newInstance( )方法呢?
即有的jdbc连接数据库的写法里是Class.forName(xxx.xx.xx);而有一些:Class.forName(xxx.xx.xx).newInstance(),为什么会有这两种写法呢?
Class.forName("");
作用是要求JVM查找并加载指定的类,如果在类中有静态初始化器的话,JVM必然会执行该类的静态代码段。
而在JDBC规范中明确要求这个Driver类必须向DriverManager注册自己,即任何一个JDBCDriver的Driver类的代码都必须类似如下:
复制收展Javapublic classMyJDBCDriver implements Driver {
static{
DriverManager.registerDriver(new MyJDBCDriver());
}
}
- 1
- 2
- 3
- 4
- 5
既然在静态初始化器的中已经进行了注册,所以我们在使用JDBC时只需要Class.forName(XXX.XXX);就可以了。