import java.sql.*;
public class JDBC {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//1.注册驱动
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","???");
//3.获取数据库操作对象
statement = connection.createStatement();
//4.执行sql
String sql = "select * from table_name";
resultSet = statement.executeQuery(sql);
//5.处理查询结果集
while(resultSet.next()){
int c1 = resultSet.getInt("id");
int c2 = resultSet.getInt("column2");
int c3 = resultSet.getInt("column3");
System.out.println(c1 + "," + c2 + "," + c3);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//4.释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
SQL语句
insert into table_name(id, column2, column3) VALUES(1,2,3),(4,5,6),(7,8,9);