![]() |
#2
林月儿2019-03-27 08:55
|

import import import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
import org.junit.Test;
public class JDBCTest1 {
/**
* ResultSet:结果集,封装了使用JDBC进行查询的结果
* 1、调用Statement对象的ExecuteQuery(sql)可以的到结果集
* 2、ResultSet 返回的实际是一张数据比哦啊,有一个指针指向数据表的第一行的前面
* 可以调用next()方法检测下一行是否有效,若有效则该方法返回true,且指针下移,
* 相当于Iterator对象的hasNext()和Next()方法的结合体
*
*3、当指针对位到一行时,可以通过调用getXxx(index)或者getXxx(conlumnname)
* 获取每一列的遏制,例如getInt(1),getString("name")
*
*4、ResultSet也需要进行关闭
*/
@Test
public void testResultSet() {
//获取id=4的customer数据表的记录,并打印
Connection conn =null;
Statement statement =null;
ResultSet rs =null;
try {
//1、获取Connection
conn = JDBCTools.getConnection();
//2、获取Statement
statement = conn.createStatement();
//3、准备Sql
String sql = "SELECT id,NAME,email,birth \r\n" +
"FROM customers WHERE id=1;";
//4、执行查询,得到ResultSet
rs = statement.executeQuery(sql);
//5、处理ResultSet
if(rs.next()) {
int id =rs.getInt(1);
String name =rs.getString("name");
String email =rs.getString(3);
Date birth = rs.getDate(4);
System.out.println(id);
System.out.println(name);
System.out.println(email);
System.out.println(birth);
}
//6、关闭数据库资源
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCTools.releaseResouce(rs, statement, conn);
}
}
}
以下是工具类程序:import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
import org.junit.Test;
public class JDBCTest1 {
/**
* ResultSet:结果集,封装了使用JDBC进行查询的结果
* 1、调用Statement对象的ExecuteQuery(sql)可以的到结果集
* 2、ResultSet 返回的实际是一张数据比哦啊,有一个指针指向数据表的第一行的前面
* 可以调用next()方法检测下一行是否有效,若有效则该方法返回true,且指针下移,
* 相当于Iterator对象的hasNext()和Next()方法的结合体
*
*3、当指针对位到一行时,可以通过调用getXxx(index)或者getXxx(conlumnname)
* 获取每一列的遏制,例如getInt(1),getString("name")
*
*4、ResultSet也需要进行关闭
*/
@Test
public void testResultSet() {
//获取id=4的customer数据表的记录,并打印
Connection conn =null;
Statement statement =null;
ResultSet rs =null;
try {
//1、获取Connection
conn = JDBCTools.getConnection();
//2、获取Statement
statement = conn.createStatement();
//3、准备Sql
String sql = "SELECT id,NAME,email,birth \r\n" +
"FROM customers WHERE id=1;";
//4、执行查询,得到ResultSet
rs = statement.executeQuery(sql);
//5、处理ResultSet
if(rs.next()) {
int id =rs.getInt(1);
String name =rs.getString("name");
String email =rs.getString(3);
Date birth = rs.getDate(4);
System.out.println(id);
System.out.println(name);
System.out.println(email);
System.out.println(birth);
}
//6、关闭数据库资源
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCTools.releaseResouce(rs, statement, conn);
}
}
}

import import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCTools {
/**
* 关闭statement,connection
*
* @param statement
* @param conn
*/
public static void releaseResouce (ResultSet rs,
Statement statement,Connection conn) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(statement!=null) {
try {
//5、关闭statement对象
statement.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) {
try {
//2、关闭连接
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 操作JDBC的工具类,其中封装了一些工具方法
*
* Version 1
*
* 1.获取连接 的方法
* 通过读取配置文件从数据库获取一个连接
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
//1、准备连接数据库的4个字符串。
//1)、创建Properties对象
Properties properties = new Properties();
//2)、获取jdbc.properties对应的输入流
InputStream in =
JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
//3)、加载2)对应的输入流
properties.load(in);
//4)、具体确定user,password等4个字符串
String user = properties.getProperty("root");
String password = properties.getProperty("1230");
String jdbcUrl = properties.getProperty("jdbcUrl");
String driver = properties.getProperty("driver");
//2、加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块
Class.forName(driver);
//3、通过DriverManager的getConnection()方法获取数据库连接
return DriverManager.getConnection(jdbcUrl, user, password);
}
}
以下是jdbc.Properties文件的内容import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCTools {
/**
* 关闭statement,connection
*
* @param statement
* @param conn
*/
public static void releaseResouce (ResultSet rs,
Statement statement,Connection conn) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(statement!=null) {
try {
//5、关闭statement对象
statement.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) {
try {
//2、关闭连接
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 操作JDBC的工具类,其中封装了一些工具方法
*
* Version 1
*
* 1.获取连接 的方法
* 通过读取配置文件从数据库获取一个连接
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
//1、准备连接数据库的4个字符串。
//1)、创建Properties对象
Properties properties = new Properties();
//2)、获取jdbc.properties对应的输入流
InputStream in =
JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
//3)、加载2)对应的输入流
properties.load(in);
//4)、具体确定user,password等4个字符串
String user = properties.getProperty("root");
String password = properties.getProperty("1230");
String jdbcUrl = properties.getProperty("jdbcUrl");
String driver = properties.getProperty("driver");
//2、加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块
Class.forName(driver);
//3、通过DriverManager的getConnection()方法获取数据库连接
return DriverManager.getConnection(jdbcUrl, user, password);
}
}
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/atguigu
user=root
password=1230
jdbcUrl=jdbc:mysql://localhost:3306/atguigu
user=root
password=1230