Get the App
SLTechnology News&Howtos  ›  Database  › 

[MYSQL Series] [basic Edition] Chapter 1 installation and testing of MYSQL

Shulou Source: shulou.com Published: 2022-06-01 10:19:27 10月23日 Update

< 60;START TRANSACTION; --开启事务,或者BEGIN; COMMIT; --提交ALTER TABLE tablenew DROP table_other; INSERT INTO tablenew VALUES('kk', 'll');ROLLBACK; --回滚========================================高阶=============================================import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;/** * JDBC demo * */public class JDBCDemo { public static void main(String[] args) throws Exception { //注册驱动// DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //加载驱动的方式 Class.forName("com.mysql.jdbc.Driver"); Properties info = new Properties(); info.setProperty("user", "root"); info.setProperty("password", "gdadmin"); //建立连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tablenew", info); // Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tablenew", "root", "gdadmin");// Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tablenew?user=root&password=gdadmin"); //获得执行sql语句的对象 Statement stmt = conn.createStatement(); //执行sql,获得结果集 String sql = "select stu_id, stu_name from tablenew"; //执行了sql,并且得到了结果集 ResultSet rs = stmt.executeQuery(sql); //处理结果集 while(rs.next()){ System.out.println(rs.getObject(1)+"\t"); System.out.println(rs.getObject("table_score") + "\t"); } //关闭资源 rs.close(); //结果集 stmt.close(); //小货车 conn.close(); //桥梁 }}=========================================================================================第一部分:测试类import java.sql.ResultSet;import java.sql.Statement;import java.util.Scanner;public class JDBCTest { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); System.out.println("请输入登录用户名"); String name = sc.nextLine(); System.out.println("请输入登录密码"); String password = sc.nextLine(); Statement st = DButil.getstsm(); String sql = "SELECT table_score from tablenew WHERE table_score >

80; "; ResultSet rs = st.executeQuery (sql); if (rs.next ()) {System.out.println (" Dear vip, welcome to join us! ") ;} else {System.out.println ("No visitors!!") Part 2: method class import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;/** * DButil.java * * / public class DButil {/ / encapsulates a static method that is used to start a connection to the sql database public static Statement getstsm () throws Exception {Class.forName ("com.mysql.jdbc.Driver") / / load driver / / get connection Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/test1", "root", "gdadmin"); / / return the connection result expression return conn.createStatement ();}}

Tags: Data transaction query level class isolation database information student file view management condition grouping test number of people classmate engine grade folder Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux MariaDB macOS Shulou Technology