mysqltestDemo
import java.sql.*; public class mysqltestdemo { public static void main(String[] args) throws Exception { // JDBC的使用 // 1.通过反射加在 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 Connection connection = DriverManager.getConnection("jdbc:mysql://master:3306/shujia", "root", "123456"); // 3.创建执行器,用来执行SQL语句 // 1.createStatement() // 2.prepareStatement() Statement statement = connection.createStatement(); String sql ="select * from student"; // 4.执行SQL语句 ResultSet rs = statement.executeQuery(sql); // 5.获取结果 System.out.println(rs); // 6.关闭 rs.close(); statement.close(); connection.close(); }
import java.sql.*; public class mysqltestdemo { public static void main(String[] args) throws Exception { // JDBC的使用 // 1.通过反射加在 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 Connection connection = DriverManager.getConnection("jdbc:mysql://master:3306/shujia", "root", "123456"); // 3.创建执行器,用来执行SQL语句 // 1.createStatement() // 2.prepareStatement() Statement statement = connection.createStatement(); String sql ="select * from student"; // 4.执行SQL语句 ResultSet rs = statement.executeQuery(sql); // 5.获取结果 while(rs.next()){ System.out.println(rs.getInt("id")); System.out.println(rs.getString("name")); System.out.println(rs.getInt("sex")); System.out.println(rs.getString("age")); } // 6.关闭 rs.close(); statement.close(); connection.close(); } }
mysqlInsert
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.util.Scanner; public class mysqlnsert { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); String name = scanner.next(); int age=scanner.nextInt(); String sex=scanner.next(); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://master:3306/shujia", "root", "123456"); Statement statement = connection.createStatement(); String sql="insert into student(name,age,sex) values(\""+name+"\","+age+",\""+sex+"\")"; int i = statement.executeUpdate(sql); System.out.println(i); if(i==1){ System.out.println("注册成功"); }else{ System.out.println("注册失败"); } statement.close(); connection.close(); } }
mysqlDelete
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class mysqlDelete { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://master:3306/shujia", "root", "123456"); Statement statement = connection.createStatement(); String sql="delete from student where name=‘zhontg‘"; int i = statement.executeUpdate(sql); System.out.println(i); statement.close(); connection.close(); } }
msyqlUpdate
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class mysqlDelete { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://master:3306/shujia", "root", "123456"); Statement statement = connection.createStatement(); String sql="delete from student where name=‘zhontg‘"; int i = statement.executeUpdate(sql); System.out.println(i); statement.close(); connection.close(); } }
Login01
package mysql.statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class Login01 { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); System.out.println("请输入用户:"); String username = scanner.next(); System.out.println("请输入密码:"); String password = scanner.next(); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://master:3306/shujia", "root", "123456"); Statement statement = connection.createStatement(); // 1.通过username去mysql中查找有没有这一条记录(存在:输入密码,不存在:报错) // 2.存在之后,用查到的密码匹配输入的密码 String sql="select * from user where username=‘"+username+"‘"; ResultSet rs = statement.executeQuery(sql); if(!rs.next()){ System.out.println("用户输出错误"); // 结束 } // 匹配密码 String password1 = rs.getString("password"); if(password==null || !password.equals(password1)){ System.out.println("密码不匹配失败"); }else { System.out.println("登陆成功"); } rs.close(); statement.close(); connection.close(); } }
package mysql.statement; import java.sql.*; import java.util.Scanner; public class Login02 { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); System.out.println("请输入用户:"); String username = scanner.nextLine(); System.out.println("请输入密码:"); String password = scanner.next(); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://master:3306/shujia", "root", "123456"); String sql="select * from user where username=?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1,username); // 1.通过username去mysql中查找有没有这一条记录(存在:输入密码,不存在:报错) // 2.存在之后,用查到的密码匹配输入的密码 ResultSet rs = statement.executeQuery(); if(!rs.next()){ System.out.println("用户输出错误"); // 结束 } // 匹配密码 String password1 = rs.getString("password"); if(password==null || !password.equals(password1)){ System.out.println("密码不匹配失败"); }else { System.out.println("登陆成功"); } rs.close(); statement.close(); connection.close(); } }
原文:https://www.cnblogs.com/Xinyi-bigdata/p/15240767.html