Java 实现Mysql操作,基础的增删改查
package Mysql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class startClass{ private String host="192.168.0.8"; private String DbName="java_chap02"; private String Uid="root"; private String Pwd="AAA@123"; private Connection conn;//声明Connection引用 private Statement stmt;//声明Statement引用 private ResultSet rs;//声明结果集引用 /** * 配置和打开数据库连接 */ public void initialConnection() { try { System.out.printf("Start Connect \n"); Class.forName("com.mysql.cj.jdbc.Driver"); //jdbc mysql连接驱动 String URL= String.format("jdbc:mysql://%s:3306/%s?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT", host,DbName); System.out.printf(URL+" \n"); conn=DriverManager.getConnection(URL,Uid,Pwd); stmt=conn.createStatement(); System.out.printf("Connect Suc \n"); } catch(SQLException e) { System.out.printf("Connect Err \n"); e.printStackTrace(); } catch(ClassNotFoundException e) { System.out.printf("Connect Err \n"+e.getMessage()); e.printStackTrace(); } } /** * 关闭连接 */ public void closeConn() { try { if(rs!=null){rs.close();} if(stmt!=null){stmt.close();} if(conn!=null){conn.close();} System.out.printf("CloseConnect Suc \n"); } catch(SQLException e) { System.out.printf("CloseConnect Err \n"+e.getMessage()); } } /** * 插入数据方法 */ public void setInser() { String sql=""; try { startClass tem=new startClass(); tem.initialConnection(); System.out.println("begin setInser 1"); sql="SELECT MAX(stu_id) as newKey from student"; long KEY=0; tem.rs=tem.stmt.executeQuery(sql); if(tem.rs.next()){ KEY= tem.rs.getLong(1); } for (int i = 0; i < 5; i++) { System.out.println("begin setInser::"+i); sql="INSERT into student VALUES ('"+(KEY+1+i)+"','"+i+"','"+i+"')"; System.out.println(sql); var dd= tem.stmt.execute(sql); System.out.println(dd); } tem.closeConn();//关闭数据库连接 } catch (Exception ea) { // TODO: handle exception System.out.println(ea); } } /** * 修改数据方法 */ public void setUpd() { String sql=""; try { startClass tem=new startClass(); tem.initialConnection(); sql="SELECT MAX(stu_id) as newKey from student"; long KEY=0; tem.rs=tem.stmt.executeQuery(sql); if(tem.rs.next()){ KEY= tem.rs.getLong(1); } sql="UPDATE student SET stu_gender='999' WHERE stu_id='"+KEY+"'"; System.out.println(sql); var dd= tem.stmt.execute(sql); tem.closeConn();//关闭数据库连接 } catch (Exception ea) { // TODO: handle exception System.out.println(ea); } } /** * 删除数据方法 */ public void setDel() { String sql=""; try { startClass tem=new startClass(); tem.initialConnection(); sql="SELECT MAX(stu_id) as newKey from student"; long KEY=0; tem.rs=tem.stmt.executeQuery(sql); if(tem.rs.next()){ KEY= tem.rs.getLong(1); } sql="delete from student WHERE stu_id='"+(KEY-2)+"'"; System.out.println(sql); var dd= tem.stmt.execute(sql); tem.closeConn();//关闭数据库连接 } catch (Exception ea) { // TODO: handle exception System.out.println(ea); } } public static void main(String args[]){ startClass tem=new startClass(); String[] message=new String[3]; String stu_id="103317012121"; try { //初始化数据库连接并更改密码 tem.initialConnection(); String sql="select stu_id,stu_name,stu_gender from student where stu_id='"+stu_id+"'"; tem.rs=tem.stmt.executeQuery(sql); if(tem.rs.next()){ message[0]=tem.rs.getString(1); message[1]=new String(tem.rs.getString(2).getBytes("gb2312")); message[2]=new String(tem.rs.getString(3).getBytes("gb2312")); } tem.closeConn();//关闭数据库连接 } catch(Exception ea) { ea.printStackTrace(); } System.out.println("begin setInser"); tem.setInser(); System.out.println("end setInser"); System.out.println("begin setUpd"); tem.setUpd(); System.out.println("end setUpd"); System.out.println("begin setDel"); tem.setDel(); System.out.println("end setDel"); /** for(int i=0;i<3;i++) System.out.printf(message[i]+"\n"); * */ } }
执行结果