package com.qf.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class TestJDBC { public static void main(String[] args) throws Exception { //1,注册驱动,加载驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //2,获得连接 String url = "jdbc:mysql://localhost:3306/db_students"; String user = "root"; String password = "密码"; Connection connection = DriverManager.getConnection(url, user, password); if (connection != null) { System.out.println("数据库连接成功"); } else { System.out.println("连接失败"); } //3,获得执行sql语句的对象 Statement statement = connection.createStatement(); //4,编写sql语句,执行sql语句 String sql = "insert into t_students(stu_id,stu_name,stu_age,stu_gender) values('1002','李四',20,'男')"; int result = statement.executeUpdate(sql); //5,处理接受结果 if (result == 1) { System.out.println("success"); } else { System.out.println("fail"); } //6,释放资源,先开后关 statement.close(); connection.close(); } }
这是一个比较全的jdbc增删改的例子
package com.qf.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class QueryJDBC2 { public static void main(String[] args) throws Exception { //1,注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //2,获取连接对象 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_students", "root", "密码"); //3,获取执行sql语句对象 Statement statement = connection.createStatement(); //4,执行sql语句,接收结果 ResultSet resultSet = statement.executeQuery("select * from t_students"); //5,处理结果e while (resultSet.next()) {//判断下一行是否有数据 String stu_id = resultSet.getString("stu_id"); String stu_name = resultSet.getString("stu_name"); String stu_age = resultSet.getString("stu_age"); String stu_gender = resultSet.getString("stu_gender"); System.out.println(stu_id + "\t" + stu_name + "\t" + stu_age + "\t" + stu_gender); } //6,释放资源 resultSet.close(); statement.close(); connection.close(); } }
这是一个比较全的查的例子
在这里我祝贺大家学有所成