通过 JDBC 向指定的数据表中插入一条记录,查询记录
//通过 JDBC 向指定的数据表中插入一条记录
/*
* 1.Statement: 用于执行SQl语句的对象
* 通过Connection 的createStatement()方法来获取
* 通过executeUptate(sql) 可执行sql语句
* 传入的sql 可以是 insert , update, delete 但不能是select
* 2.Connection 和 Statement 需要关闭
* 需要在finally中关闭
* 3.关闭的顺序:先关闭后获取的,即先关闭 Statement 后关闭 Connection
*/
@Test
public void testStatement() throws Exception{
//1.获取数据库连接
Connection conn;
Statement statement;
conn = null;
statement = null;
try {
conn = getConnection();
//3.SQL语句
String sql =
"insert into mytable values(23454, ‘BBB‘)";
//4.执行
//1) 获取操作SQL语句的Statement对象:调用Connection 的createStatement()方法
statement = conn.createStatement();
//2) 调用 Statement 对象的executeUpdate(sql)执行 SQL语句进行插入
statement.executeUpdate(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(statement != null)
//5. 关闭 Statement对象
statement.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(conn != null)
//2.关闭连接
conn.close();
}
}
}
public Connection getConnection() throws Exception{
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
InputStream in =
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
Driver driver =
(Driver) Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}
/*ResultSet: 结果集。封装了使用 JDBC 进行查询的结果
* 1.调用 Statement 对象的 executeQuery(sql) 可以得到结果集。
* 2.ResultSet 返回的实际上就是一张数据表。有一个指针指向数据表的第一行的前面
* 可以调用 next()方法检测下一行是否有效。若有效该方法返回 true,且指针下移相当于
* Iterator 对象的 hasNext() 和next()方法的结合体
* 3.当指针对位到一行时,可以通过调用getXxx(index) 或 getXxx(columnName)来
* 获取每一列的值。例如:getInt(12345),getString(“name”)
* 4.ResultSet 当然也需要关闭
* */
@Test
public void testResultSet(){
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
conn = JDBCTools.GetConnection();
statement = conn.createStatement();
String sql = "select id, name from mytable";
rs = statement.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString("name");
System.out.println(id);
System.out.println(name);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools.release(rs, statement, conn);
}
}
public class JDBCTools {
public static void release(ResultSet rs, Statement statement, Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null)
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
if(conn != null)
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
/*
*关闭Statement 和 Connection */
public static void release(Statement statement, Connection conn){
if(statement != null)
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
if(conn != null)
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
/*
* 1. 获取连接的方法
* 通过读配置文件从数据库服务器获取一个连接
*/
public static Connection GetConnection() throws Exception{
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
InputStream in =
JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
Driver driver =
(Driver) Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。