简单的增删改查
package com.company;
import java.sql.*;
import java.util.Date;
public class hello01jdbc {
private static String driver="com.mysql.cj.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/scott?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT";
private static final String user = "root";
private static final String password = "123456";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
}
private static void dql() throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection connection =DriverManager.getConnection(url,user,password);
Statement statement =connection.createStatement();
String sql= "select * from emp";
ResultSet resultSet=statement.executeQuery(sql);
while (resultSet.next()) {
int empno = resultSet.getInt("empno");
String ename=resultSet.getString("ename");
String job=resultSet.getString("job");
int mgr = resultSet.getInt("mgr");
Date hiredate =resultSet.getDate("hiredate");
int sal = resultSet.getInt("sal");
double comm =resultSet.getDouble("comm");
int deptnpo = resultSet.getInt("deptno");
System.out.println(empno+"\t"+ename+"\t"+job+"\t"+mgr+"\t"+hiredate+"\t"+sal+"\t"+comm+"\t"+deptnpo);
}
}
private static void dml() throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection connection = DriverManager.getConnection(url,user,password);
Statement statement = connection.createStatement();
String sql="insert into dept values(11,'行政部','北京')";
int rows = statement.executeUpdate(sql);
System.out.println("hello01jdbc.main[Affected rows:"+ rows+"]");
}
}
|