原始jdbc模板

package com.tarena.mybatis.jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;




public class JdbcTest {

public static void main(String[] args) {
Connection conn = null ;
PreparedStatement statement = null;
ResultSet resultSet = null;

try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///mydb?characterEncoding=utf8","root", "123");
String sql = "select * from user where username = ?";
statement = conn.prepareStatement(sql);
statement.setString(1, "王五");
resultSet = statement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getString("id")+","+resultSet.getString("username"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}

}

你可能感兴趣的:(原始jdbc模板)