新建 Directory 名称为lib
复制 黏贴 选中lib右击 add as Library
ok
查询
package com.zhongruan;
import java.sql.*;
public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建连接
Connection connection =DriverManager.getConnection( “jdbc:mysql://localhost/zjgm?user=root&password=123456”);
System.out.println(“创建连接成功”);
//3.写sql
String sql=“select * from stu”;
//4.得到statement对象
PreparedStatement statement = connection.prepareStatement(sql);
//5.执行sql
ResultSet resultSet = statement.executeQuery();
//6.处理结果集
while (resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getNString(2);
int age =resultSet.getInt(3);
System.out.println(id+name+age);
}
//7.关闭资源
resultSet.close();
statement.close();
connection.close();
}
}
删除
package com.zhongruan;
import java.sql.*;
public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(“com.mysql.jdbc.Driver”);
Connection connection =DriverManager.getConnection( "jdbc:mysql://localhost/zjgm?user=root&password=123456");
System.out.println("创建连接成功");
// String sql="select * from stu";
String sql="delete from stu where id=1";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
// while (resultSet.next()){
// int id=resultSet.getInt(1);
// String name=resultSet.getNString(2);
// int age =resultSet.getInt(3);
// System.out.println(id+name+age);
// }
//resultSet.close();
statement.close();
connection.close();
}
}
添加
package com.zhongruan;
import java.sql.*;
public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(“com.mysql.jdbc.Driver”);
Connection connection =DriverManager.getConnection( "jdbc:mysql://localhost/zjgm?user=root&password=123456");
System.out.println("创建连接成功");
// String sql="select * from stu";
String sql="insert into stu(id,name,age) values ('1','haha',18)";
//insert into student (name,money,sex,phone) values ('hk',10000,'男',188);
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
// while (resultSet.next()){
// int id=resultSet.getInt(1);
// String name=resultSet.getNString(2);
// int age =resultSet.getInt(3);
// System.out.println(id+name+age);
// }
//resultSet.close();
statement.close();
connection.close();
}
}
修改
package com.zhongruan;
import java.sql.*;
public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(“com.mysql.jdbc.Driver”);
Connection connection =DriverManager.getConnection( "jdbc:mysql://localhost/zjgm?user=root&password=123456");
System.out.println("创建连接成功");
// String sql="select * from stu";
String sql="update stu set age=12 where name='haha'";
// update student set money=110 where name='hk';
//insert into student (name,money,sex,phone) values ('hk',10000,'男',188);
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
// while (resultSet.next()){
// int id=resultSet.getInt(1);
// String name=resultSet.getNString(2);
// int age =resultSet.getInt(3);
// System.out.println(id+name+age);
// }
//resultSet.close();
statement.close();
connection.close();
}
}