JDBC练习一
import java.sql.*;
public class JDBCTest01 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Driver driver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
String url = "jdbc:mysql://127.0.0.1:3306/mydb";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象" + conn);
stmt = conn.createStatement();
int count = stmt.executeUpdate("insert into student values(5,'田七')");
System.out.println(count == 1 ? "保存成功":"保存失败");
} catch(SQLException e) {
e.printStackTrace();
} finally {
if(stmt != null) {
try {
stmt.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
运行结果: