jdbc连接mysql数据库以及CRUD_MySQL
程序员文章站
2022-06-09 13:07:18
...
"jdbc:mysql://ip:端口/数据库名称", 用户名,密码
lJdbc程序中的Statement对象用于向数据库发送SQL语句, Statement对象常用方法: executeQuery(String sql) :用于向数据发送查询语句。 executeUpdate(String sql):用于向数据库发送insert、update或delete语句 execute(String sql):用于向数据库发送任意sql语句 addBatch(String sql) :把多条sql语句放到一个批处理中。 executeBatch():向数据库发送一批sql语句执行。
package cn.outofmemory.test;import java.sql.Connection;import java.sql.DriverManager;public class Mysql { public static void main(String arg[]) { try { Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL System.out.print("yes"); } catch (Exception e) { System.out.print("MYSQL ERROR:" + e.getMessage()); } }}
//插入一条语句mport java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class Mysql { /** * 入口函数 * @param arg */ public static void main(String arg[]) { try { Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL Statement stmt; //创建声明 stmt = con.createStatement(); //新增一条数据 stmt.executeUpdate("INSERT INTO user (username, password) VALUES ('init', '123456')"); ResultSet res = stmt.executeQuery("select LAST_INSERT_ID()"); int ret_id; if (res.next()) { ret_id = res.getInt(1); System.out.print(ret_id); } } catch (Exception e) { System.out.print("MYSQL ERROR:" + e.getMessage()); } }}
//删除和更新数据import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class Mysql { /** * 入口函数 * @param arg */ public static void main(String arg[]) { try { Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL Statement stmt; //创建声明 stmt = con.createStatement(); //新增一条数据 stmt.executeUpdate("INSERT INTO user (username, password) VALUES ('init', '123456')"); ResultSet res = stmt.executeQuery("select LAST_INSERT_ID()"); int ret_id; if (res.next()) { ret_id = res.getInt(1); System.out.print(ret_id); } //删除一条数据 String sql = "DELETE FROM user WHERE id = 1"; long deleteRes = stmt.executeUpdate(sql); //如果为0则没有进行删除操作,如果大于0,则记录删除的条数 System.out.print("DELETE:" + deleteRes); //更新一条数据 String updateSql = "UPDATE user SET username = 'xxxx' WHERE id = 2"; long updateRes = stmt.executeUpdate(updateSql); System.out.print("UPDATE:" + updateRes); } catch (Exception e) { System.out.print("MYSQL ERROR:" + e.getMessage()); } }}
//查询数据import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.Statement;public class Mysql { /** * 入口函数 * @param arg */ public static void main(String arg[]) { try { Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL Statement stmt; //创建声明 stmt = con.createStatement(); //新增一条数据 stmt.executeUpdate("INSERT INTO user (username, password) VALUES ('init', '123456')"); ResultSet res = stmt.executeQuery("select LAST_INSERT_ID()"); int ret_id; if (res.next()) { ret_id = res.getInt(1); System.out.print(ret_id); } //删除一条数据 String sql = "DELETE FROM user WHERE id = 1"; long deleteRes = stmt.executeUpdate(sql); //如果为0则没有进行删除操作,如果大于0,则记录删除的条数 System.out.print("DELETE:" + deleteRes); //更新一条数据 String updateSql = "UPDATE user SET username = 'xxxx' WHERE id = 2"; long updateRes = stmt.executeUpdate(updateSql); System.out.print("UPDATE:" + updateRes); //查询数据并输出 String selectSql = "SELECT * FROM user"; ResultSet selectRes = stmt.executeQuery(selectSql); while (selectRes.next()) { //循环输出结果集 String username = selectRes.getString("username"); String password = selectRes.getString("password"); System.out.print("/r/n/r/n"); System.out.print("username:" + username + "password:" + password); } } catch (Exception e) { System.out.print("MYSQL ERROR:" + e.getMessage()); } }}
推荐阅读
-
完美解决MySQL通过localhost无法连接数据库的问题
-
mysql的jdbc配置(mysql数据库备份讲解)
-
nodejs进阶(6)—连接MySQL数据库示例
-
JSP连接MySql/MS SQL Server/Oracle数据库连接方法[整理]
-
JDBC连接mysql乱码异常问题处理总结
-
Python连接mysql数据库及python使用mysqldb连接数据库教程
-
JDBC连接MySQL5.7的方法
-
myeclipse连接mysql数据库详细步骤(实战mysql数据库引擎)
-
详解MySQL数据库--多表查询--内连接,外连接,子查询,相关子查询
-
详解使用navicat连接远程linux mysql数据库出现10061未知故障