JDBC的学习
--第一个JDBC
import java.sql.*;
public class JDBCTest02{
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//1、注册驱动
// DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName("com.mysql.jdbc.Driver");
//2、获取连接
String url = "jdbc:mysql://localhost:3306/bjpowernode?useUnicode=true&characterEncoding=utf8&useSSL=false";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url,username,password);
//3、获得操作对象
stmt = conn.createStatement();
//4、写sql语句
String sql = "INSERT INTO dept(DEPTNO,DNAME,LOC) VALUES(260,'瓜','重庆');";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1? "插入成功":"插入失败");
//5、处理结果集
} catch(SQLException | ClassNotFoundException e){
e.printStackTrace();
}finally{
//5、释放资源
if (stmt != null) {
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if (conn != null) {
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
