标签:
class JdbcDemo
{
pubic static void jdbcTest( String sql)
{
Connection con=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql//locallhost:3306/test?charactorEncoding=utf-8","root","123");
preparedStatement=con.prepareStatement(sql);//Statement statement=con.createStatement()
preparedStatement.setString(1,"tom");//给sql语句设置参数
resultSet=preparedStatement.excuteQuery();
while(resultSet.next())
{
system.out.println(resultSet.getInt("id"));
System.out.println(resultSet.getString("username"));
}
catch( Exception e)
{
e.printStackTrace();
}
finally
{
if(resultSet!=null)
{
try{
resultSet.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/chuanqimessi/p/4761467.html