java连接数据库和jdbc的增删改查操作
建立数据库和表
打开数据库软件(SQLyog - 64 bit Trial),创建一个新的数据库并建立一种名为student的表。
设置表的字段名,并添加一条数据。
jdbc连接数据库
拷贝架包
建立一个lib文件夹并将所需要的架包复制到lib目录下。
右键点击选中的架包,选中add as library选项把该架包添加到自己项目里。
jdbc连接的7步骤
1.建立驱动
Class.forName("com.mysql.jdbc.Driver");
2.创建连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=xiayin0001&characterEncoding=utf-8&useSSL=true");
System.out.println("创建成功");
3.写sql代码(以查询为例)
String sql="select * from student";
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.getString(2);
int Age=resultSet.getInt(3);
System.out.println("id:"+ID+" name:"+Name+" age:"+Age);
}
结果演示
7.关闭资源
resultSet.close();
statement.close();
connection.close();
关于jdbc增删改查的操作
1.增加数据
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=xiayin0001&characterEncoding=utf-8&useSSL=true");
String sql="INSERT INTO student (ID,Name,Age) VALUES(4,'Nacy',19)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
运行前后表格数据对比
2.删除数据
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=xiayin0001&characterEncoding=utf-8&useSSL=true");
String sql = "DELETE FROM student WHERE student.Name = 'Nacy'";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
运行前后表格数据对比
3.更改数据
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=xiayin0001&characterEncoding=utf-8&useSSL=true");
String sql = "UPDATE student SET student.Age = '99' where Name='Jack'";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
运行前后表格数据对比
4.查询数据
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=xiayin0001&characterEncoding=utf-8&useSSL=true");
System.out.println("创建成功");
String sql="select * from student";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()){
int ID=resultSet.getInt(1);
String Name=resultSet.getString(2);
int Age=resultSet.getInt(3);
System.out.println("id:"+ID+" name:"+Name+" age:"+Age);
}
结果演示
完整代码
package xy.zjgm;
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=xiayin0001&characterEncoding=utf-8&useSSL=true");
System.out.println("创建成功");
//增加数据
String sql="INSERT INTO student (ID,Name,Age) VALUES(4,'Nacy',19)";
//删除数据
// String sql = "DELETE FROM student WHERE student.Name = 'Nacy'"
//改变数据
// String sql = "UPDATE student SET student.Age = '20' where Name='Jack'";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
//查询数据
// String sql="select * from student";
// PreparedStatement statement = connection.prepareStatement(sql);
// ResultSet resultSet = statement.executeQuery();
// while (resultSet.next()){
// int ID=resultSet.getInt(1);
// String Name=resultSet.getString(2);
// int Age=resultSet.getInt(3);
// System.out.println("id:"+ID+" name:"+Name+" age:"+Age);
// }
// resultSet.close();
statement.close();
connection.close();
}
}