jdbc连接数据库实例详解
JDBC简介
JDBC全称为:JavaDataBaseConnectivity(java数据库连接),可以为多种数据库提供填统一的访问。JDBC是sun开发的一套数据库访问编程接口,是一种SQL级的API。它是由java语言编写完成,所以具有很好的跨平台特性,使用JDBC编写的数据库应用程序可以在任何支持java的平台上运行,而不必在不同的平台上编写不同的应用程序。
JDBC编程步骤
(1)加载驱动程序:
下载驱动包:http://dev.mysql.com/downloads/connector/j/
解压,得到jar文件。将该文件复制到Java工程目录JavaResources/Libraries/下,→buildpath。
(2)获得数据库连接
(3)创建Statement对象:
(4)向数据库发送SQL命令
(5)处理数据库的返回结果(ResultSet类)
packagecom.baidu.emp.jdbcTest;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.Statement;
importcom.mysql.jdbc.Driver;
/**
*开始使用jdbc连接数据库
*@authorAdmin
*
*/
publicclassTest001{
publicstaticvoidmain(String[]args)throwsException{
/**
*加载驱动
*/
//方法一:
/*
*importjava.sql.DriverManager;importcom.mysql.jdbc.Driver;
*/
//Driverdriver=newDriver();
//DriverManager.registerDriver(driver);
//方法二:(推荐使用)
Class.forName("com.mysql.jdbc.Driver");
/**
*创建链接
*/
Stringurl="jdbc:mysql://localhost:3306/testjdbc";
Stringuser="root";
Stringpassword="root";
Connectionconnection=DriverManager.getConnection(url,user,password);
//创建statement对象
Statementstatement=connection.createStatement();
/**
*执行SQL,获取结果集
*/
Stringsql="select*fromtest01";
ResultSetresult=statement.executeQuery(sql);
//遍历结果集
while(result.next()){
Stringname=result.getString("name");
intid=result.getInt("id");
System.out.println(name+"\t"+id);
}
/**
*关闭链接,释放资源
*/
result.close();
statement.close();
connection.close();
}
}
防止SQL注入改用prepareStatement
packagecom.boya.emp.jdbcTest;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
/**
*SQL注入,使用prepareStatement对象进行预编译
*@authorAdmin
*
*/
publicclassTest002{
publicstaticvoidmain(String[]args)throwsException{
/**
*加载驱动
*/
Class.forName("com.mysql.jdbc.Driver");
/**
*创建链接
*/
Stringurl="jdbc:mysql://localhost:3306/testjdbc";
Stringuser="root";
Stringpassword="root";
Connectionconnection=DriverManager.getConnection(url,user,password);
//写SQL
Stringsql="select*fromtest01whereid=?";
//创建statement对象,预编译
PreparedStatementstatement=connection.prepareStatement(sql);
//设置参数
statement.setInt(1,2);
/**
*执行SQL,获取结果集
*/
ResultSetresult=statement.executeQuery();
//遍历结果集
while(result.next()){
Stringname=result.getString("name");
intid=result.getInt("id");
System.out.println(name+"\t"+id);
}
/**
*关闭链接,释放资源
*/
result.close();
statement.close();
connection.close();
}
}
进行代码优化,设置配置文件,工具类,实现增删该查
增加配置文件方便修改数据库,用户登录。。。
jdbc.properties(配置文件名)
driverName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/testjdbc userName=root password=root
注意写配置文件时中间不可以有空格,引号之类的
工具类:增强了代码的复用性
packagecom.baidu.emp.utils;
importjava.io.InputStream;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.Properties;
importorg.junit.Test;
publicclassJdbcUtils{
staticStringdriverClassName;
staticStringurl;
staticStringuser;
staticStringpassword;
static{
//创建配置文件对象
Propertiesproperties=newProperties();
//加载配置文件输入流
InputStreaminputStream=JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//重新加载配置文件
try{
properties.load(inputStream);
//获取配置文件的值
driverClassName=properties.getProperty("driverName");
url=properties.getProperty("url");
user=properties.getProperty("userName");
password=properties.getProperty("password");
Class.forName(driverClassName);
}catch(Exceptione){
//抛出异常
thrownewRuntimeException(e);
}
}
/**
*获取连接
*/
@Test
publicvoidtestName()throwsException{
System.out.println(driverClassName);
}
publicstaticConnectiongetConnection(){
Connectionconnection=null;
try{
connection=DriverManager.getConnection(url,user,password);
}catch(SQLExceptione){
//抛出异常
thrownewRuntimeException(e);
}
returnconnection;
}
/**
*关闭链接,释放资源
*/
publicstaticvoidclose(Connectionconnection,PreparedStatementstatement,ResultSetresultSet){
try{
if(resultSet!=null){
resultSet.close();
}
resultSet=null;//垃圾及时清除
//注意,不要弄成死循环
close(connection,statement);
}catch(SQLExceptione){
thrownewRuntimeException(e);
}
}
/**
*增删改释放资源
*/
publicstaticvoidclose(Connectionconnection,PreparedStatementstatement){
try{
if(connection!=null){
connection.close();
}
connection=null;
if(statement!=null){
statement.close();
}
statement=null;
}catch(SQLExceptione){
thrownewRuntimeException(e);
}
}
}
测试增删改查:
packagecom.baidu.emp.jdbcTest;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
importcom.baidu.emp.utils.JdbcUtils;
/**
*使用jdbcUtils连接数据库进行增删改查
*
*@authorAdmin
*
*/
publicclassTest003{
//初始化值
Connectionconnection=null;
PreparedStatementstatement=null;
ResultSetresult=null;
@Before
publicvoidstart()throwsException{
//创建链接
connection=JdbcUtils.getConnection();
System.out.println("创建链接");
}
@After
publicvoidend()throwsException{
//关闭链接
JdbcUtils.close(connection,statement,result);
System.out.println("关闭链接");
}
/**
*插入数据
*@throwsException
*/
@Test
publicvoidadd()throwsException{
Stringsql="insertintotest01values(null,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,"李四");
intresult=statement.executeUpdate();
if(result!=0){
System.out.println("添加成功");
}
}
/**
*删除数据
*@throwsException
*/
@Test
publicvoiddel()throwsException{
Stringsql="deletefromtest01whereid=?";
statement=connection.prepareStatement(sql);
statement.setInt(1,3);
intresult=statement.executeUpdate();
if(result!=0){
System.out.println("删除成功");
}
}
/**
*修改数据
*@throwsException
*/
@Test
publicvoidchange()throwsException{
Stringsql="updatetest01setname=?whereid=?";
statement=connection.prepareStatement(sql);
statement.setString(1,"张飞");
statement.setInt(2,2);
intresult=statement.executeUpdate();
if(result!=0){
System.out.println("修改成功");
}
}
/**
*查询全部数据
*@throwsException
*/
@Test
publicvoidfindAll()throwsException{
Stringsql="selectid,namefromtest01";
statement=connection.prepareStatement(sql);
result=statement.executeQuery();
if(result.next()){
System.out.println("查询成功");
}
}
/**
*条件查询数据
*@throwsException
*/
@Test
publicvoidfindOne()throwsException{
Stringsql="selectid,namefromtest01whereid=?";
statement=connection.prepareStatement(sql);
statement.setInt(1,2);
result=statement.executeQuery();
if(result.next()){
System.out.println("查询成功");
}
}
}
以上就是相关知识以及相关代码,感谢大家对毛票票的支持。