JDBC连接数据库
import java.sql.*;
public class Test01 {
public static void main(String[] args) {
Connection connection=null;
Statement statement=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection= DriverManager.getConnection("jdbc:mysql://localhost:3307/j3071test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8","root","root");
statement=connection.createStatement();
String sql="insert into student (id,name,age,sex) values(id,'j3071',20,'男')";
String str1="insert into student (id,name,age,sex) values(id,";
String name="张三";
int age=20;
String sql1=str1+"'"+name+"',"+age+","+"'男')";
int row = statement.executeUpdate(sql);
System.out.println("受影响的行数为:"+row);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
if (rs!=null){
rs.close();
}
if (statement!=null){
statement.close();
}
if (connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}