当前位置:Gxlcms > 数据库问题 > 使用PreparedStatement接口操作数据库

使用PreparedStatement接口操作数据库

时间:2021-07-01 10:21:17 帮助过:15人阅读

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

public class Demo3 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt =null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool", "root", "0000");
System.out.println(conn);
pstmt = conn.prepareStatement("select * from student where StudentName=? and LoginPwd=?");
pstmt.setString(1, "郭靖");
pstmt.setString(2, "111111");
rs = pstmt.executeQuery();
while(rs.next()){
//查询获取字段值

}
} catch (Exception e) {
e.printStackTrace();
}finally{
//释放资源
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
pstmt = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
}

使用PreparedStatement接口操作数据库

标签:prepare   drive   java   安全性   tps   manage   dem   statement   获取   

人气教程排行