欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

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());        }    }}