mysql操作源码

package com.mysql;
import java.sql.*;
public class MysqlTest {
static final String driver="com.mysql.cj.jdbc.Driver";
static final String DB="jdbc:mysql://localhost/db10";
static final String user="root";
static final String password="lyf123456";
public static void main(String[] args){
Connection conn=null;
Statement stmt=null;
try{
Class.forName(driver);
conn=DriverManager.getConnection(DB,user,password);
stmt=conn.createStatement();
String sql="insert into Student values('scofied',45,89,100)";
stmt.executeUpdate(sql);
System.out.println("插入成功");

} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException 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();
}
}
}
}
}

posted @ 2022-12-22 18:13  Lindseyyip  阅读(49)  评论(0)    收藏  举报