mysql資料庫的串連以及增刪改查Java代碼實現(Statement版),mysqlstatement

來源:互聯網
上載者:User

mysql資料庫的串連以及增刪改查Java代碼實現(Statement版),mysqlstatement

資料庫:

create table t1(id int primary key not null auto_increment,name varchar(32),password varchar(32));insert into t1(name,password) values('admin','123');insert into t1(name,password) values('zhangsan','123');insert into t1(name,password) values('lisi','123');

Java代碼:

mysqlDao.java:

package com.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import org.junit.Test;public class mysqlDao {private static String driver="com.mysql.jdbc.Driver";     //驅動private String url="jdbc:mysql://localhost:3306/test";    //資料庫連接地址private String user="root";     //資料庫賬戶private String password="11";      //資料庫密碼private Connection connection=null;      //串連private Statement stmt=null;      //聲明private ResultSet rs=null;     //結果集private int i=-1;/* * 建立驅動 * */static{try {Class.forName(driver);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/* * 載入驅動 * */@Testpublic void connect() {// TODO Auto-generated method stubtry {connection=DriverManager.getConnection(url, user, password);if(connection!=null){System.out.println("資料庫連接成功!");}else{System.out.println("資料庫連接失敗!");}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/* * 執行sql語句 * */public void doSql(String sql) {// TODO Auto-generated method stubSystem.out.println("This Is mysqlDao.doSql() Method!");if(sql!=null){connect();try {stmt=connection.createStatement();stmt.execute(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/* * 擷取影響行數 * */public int getUpCount() {// TODO Auto-generated method stubtry {i=stmt.getUpdateCount();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return i;}/* * 擷取結果集 * */public ResultSet getRs() {try {rs=stmt.getResultSet();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return rs;}/* * 執行關閉方法 * */public void close() {// TODO Auto-generated method stubtry {if(rs!=null){rs.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(stmt!=null){stmt.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(connection!=null){try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}}

createSql.java

package com.dao;import java.sql.ResultSet;import java.sql.SQLException;import org.junit.Test;public class createSql {mysqlDao mysqldao=new mysqlDao();private String sql;private int i=-1;private ResultSet rs=null;/* * 插入資料 * */@Testpublic void insert() {// TODO Auto-generated method stubsql="insert into t1(name,password) values('lisi','1234')";    //建立sql語句mysqldao.doSql(sql);    //執行sql語句i=mysqldao.getUpCount();     //擷取影響行數if(i!=-1){System.out.println("資料插入成功!");}else{System.out.println("資料插入失敗!");}mysqldao.close();      //關閉串連}/* * 刪除資料 * */@Testpublic void delete() {// TODO Auto-generated method stubsql="delete from t1 where id=6";mysqldao.doSql(sql);i=mysqldao.getUpCount();if(i!=-1){System.out.println("資料刪除成功!");}else{System.out.println("資料刪除失敗!");}mysqldao.close();}/* * 修改資料 * */@Testpublic void update() {// TODO Auto-generated method stubsql="update t1 set name='wangwu' where id=1";mysqldao.doSql(sql);i=mysqldao.getUpCount();if(i!=-1){System.out.println("資料更新成功!");}else{System.out.println("資料更新失敗!");}mysqldao.close();}/* * 遍曆資料 * */@Testpublic void select() throws SQLException {// TODO Auto-generated method stubsql="select * from t1";mysqldao.doSql(sql);rs=mysqldao.getRs();if(rs!=null){rs.last();i=rs.getRow();if(i>0){rs.beforeFirst();while(rs.next()){String id=rs.getString("id");String name=rs.getString("name");String password=rs.getString("password");System.out.println("id:"+id+"  "+"name:"+name+"  password:"+password);}}else{System.out.println("對不起,沒有您要查詢的資訊!");}}else{System.out.println("異常...........");}mysqldao.close();}}


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.