Java鏈接本地MySQL數據庫進行增刪改查操做

package Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import entity.UserInfo;

public class JDBC {
    Connection conn;
    PreparedStatement ps;
    ResultSet rs;
    /**
     * 寫一個鏈接數據庫的方法
     */
    public Connection getConnection(){
        String url="jdbc:mysql://localhost:端口/數據庫";
        String userName="root";
        String password="密碼";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("找不到驅動!");
            e.printStackTrace();
        }
        try {
            conn=DriverManager.getConnection(url, userName, password);
            if(conn!=null){
                System.out.println("connection successful");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
                System.out.println( "connection fail");
            e.printStackTrace();
        }
        return conn;
    }
    /**
     * 寫一個查詢數據庫語句的方法
     */
    public void QuerySql(){
        //一、執行靜態SQL語句。一般經過Statement實例實現。   
        // 二、執行動態SQL語句。一般經過PreparedStatement實例實現。   
        // 三、執行數據庫存儲過程。一般經過CallableStatement實例實現。
        System.out.println("query");
        UserInfo u;
//        j.Connection();
        String sql="select * from userInfo";
        try {
            conn=getConnection();//鏈接數據庫
            ps=conn.prepareStatement(sql);// 2.建立Satement並設置參數
            rs=ps.executeQuery(sql);  // 3.ִ執行SQL語句
            // 4.處理結果集
            while(rs.next()){
                u=new UserInfo();
                u.setUserId(rs.getInt("userId"));
                u.setUserName(rs.getString("userName"));
                u.setPassword(rs.getString("password"));
                u.setRemark(rs.getString("remark"));
                System.out.println("uesrName"+u.getUserName());
            }
            System.out.println(rs.next());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //釋放資源
            try {
                rs.close();
                ps.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    @SuppressWarnings("null")
    public int add(UserInfo user){
        UserInfo u =new UserInfo();
        int row=0;
//        j.Connection();
        String sql="insert into userInfo(userName,password) values(?,?)";
        try {
            conn=getConnection();//鏈接數據庫
            ps=conn.prepareStatement(sql);// 2.建立Satement並設置參數
//            rs=ps.executeQuery();  // 3.ִ執行SQL語句,緊緊用於查找語句
            //sql語句中寫了幾個字段,下面就必須要有幾個字段
             ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassword());
            // 4.處理結果集
            row=ps.executeUpdate();
            System.out.println(row+user.getUserName()+user.getPassword());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        return row;
    }
    /**
     * @return修改數據
     */
    public int update(UserInfo user){
        UserInfo u;
        int row=0;
//        j.Connection();
        String sql="update userInfo set userName=?,password=? where userId=?";
        try {
            conn=getConnection();//鏈接數據庫
            ps=conn.prepareStatement(sql);// 2.建立Satement並設置參數
//            rs=ps.executeQuery(sql);  // 3.ִ執行SQL語句,緊緊用於查找語句
            //sql語句中寫了幾個字段,下面就必須要有幾個字段
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassword());
            ps.setInt(3, user.getUserId());
            // 4.處理結果集
            row=ps.executeUpdate();
            System.out.println(row);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //釋放資源
            try {
//                rs.close();
                ps.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return row;
    }
    /**
     * @return刪除操做
     */
    public int delete(UserInfo user){
        UserInfo u;
        int row=0;
//        j.Connection();
        String sql="delete from userInfo where userId=?";
        try {
            conn=getConnection();//鏈接數據庫
            ps=conn.prepareStatement(sql);// 2.建立Satement並設置參數
//            rs=ps.executeQuery(sql);  // 3.ִ執行SQL語句,緊緊用於查找語句
            //sql語句中寫了幾個字段,下面就必須要有幾個字段
            ps.setInt(1, user.getUserId());
            // 4.處理結果集
            row=ps.executeUpdate();
            System.out.println(row);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //釋放資源【執行完sql要記得釋放資源】
            try {
//                rs.close();
                ps.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return row;
    }
    public static void main(String[] args) {
        JDBC j=new JDBC();
//        j.getConnection();
        j.QuerySql();//在控制檯顯示出查找方法
//        UserInfo u=new UserInfo();
//        u.setUserId(5);
//        u.setUserName("cool");
//        u.setPassword("123abc");
//        j.update(u);////在控制檯顯示出修改方法
    }
}

 

 

from: http://blog.csdn.net/qianquan003/article/details/23364381?utm_source=tuicool&utm_medium=referraljava

相關文章
相關標籤/搜索