JDBC编程存在的问题
JDBC编程的完整步骤请参考:JDBC编程,这里以一个JDBC的实例类来说明:
import java.sql.*;
public class JDBC_Demo {
public static void main(String[] args) throws Exception {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
//加载mysql驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String DB_URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASS = "123456";
connection = DriverManager.getConnection(DB_URL, USER, PASS);
//获取Statement
statement = connection.createStatement();
String sql = "SELECT * FROM Student where SID=1 ";
//执行查询操作
rs = statement.executeQuery(sql);
//处理结果集
while (rs.next()) {
String sid = rs.getString("SID");
String sname = rs.getString("Sname");
String ssex = rs.getString("Ssex");
String sage = rs.getString("Sage");
System.out.println(sid + ":" + sname + ":" + ssex + ":" + sage);
}
} finally {
//关闭资源、释放连接
if (rs != null)
rs.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
}
}
}
问题分析:
-
1、在使用JDB