后台數據庫的增刪改查 項目的


連接數據庫的until幫助類

package demo1;

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

public class MyDBUtil {
    private static String className = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://192.168.0.131:9171/sisenmess-user-database";
    public static final String user = "root";  
    public static final String password = "123"; 
    
    
    public static Connection getConnection() throws ClassNotFoundException, SQLException{
        Class.forName(className);
        Connection con = DriverManager.getConnection(url,user,password);
        return con;
    }
    
    public static Statement getStatement(Connection con) throws SQLException{
        Statement st = con.createStatement();
        return st;
    }
    
    public static PreparedStatement getPreparedStatement(Connection con,String sql) throws SQLException{
        PreparedStatement ps = con.prepareStatement(sql);
        return ps;
    }
    
    public static ResultSet getResultSet(Statement st,String sql) throws SQLException{
        ResultSet rs = st.executeQuery(sql);
        /*int rs1 = st.executeUpdate(sql);*/
        return rs;
        
    }
    
    
    public static void closeAllMethod(Connection con,Statement st,ResultSet rs){
          try {
           if(rs!=null){
            rs.close();
           }
           if(st!=null){
            st.close();
           }
           if(con!=null){
            con.close();
           }
          } catch (SQLException e) {
           // TODO 自動生成 catch 塊
           e.printStackTrace();
          }
        }

}

項目中的增刪改查示例

package demo1;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;


public class BuguManger {
    public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
        
        BuguManger bugu= new BuguManger();
        //bugu.addBugu();
        //bugu.search();
        //bugu.update();
        bugu.deleteBugu();
    
    }
    public void search() throws ClassNotFoundException, SQLException, IOException{
        //查詢數據
                String sql = "";
                Connection con1 = MyDBUtil.getConnection();
                PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql);
                
            
                sql = "select * from userlist";
                ResultSet rs1 = ps.executeQuery(sql);
                while(rs1.next()){
                    String uid=rs1.getString(1);
                    String uid2=rs1.getString(2);
                    System.out.println(uid);
                }
    }
    

            
            public void addBugu() throws ClassNotFoundException, SQLException, IOException{
                //增加一條數據
                String sql = "";
                Connection con1 = MyDBUtil.getConnection();
                PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql);
                
                InputStreamReader input = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(input);
//                System.out.print("請輸入用戶名:");
//                uname = br1.readLine();
//                System.out.print("請輸入密碼:");
//                pword = br1.readLine();
//                ps.setString(1, uname);
//                ps.setString(2, pword);
                System.out.print("請輸入要添加的姓名:");
                String uname = br.readLine();
//                
//                System.out.print("請輸入要添加的密碼:");
                String pword = br.readLine();
//                
//                System.out.print("請輸入要添加的郵箱:");
                String email = br.readLine();
                sql = "insert into userlist (UserId,UserNo,UserTName) values('"+uname+"','"+pword+"','"+email+"')";
                int rs1 = ps.executeUpdate(sql);
            }
            //修改數據
            public void update() throws ClassNotFoundException, SQLException, NumberFormatException, IOException{
                InputStreamReader input = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(input);
                String sql = "";
                Connection con1 = MyDBUtil.getConnection();
                PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql);
                System.out.println("請輸入你所要修改的編號:");
                int id = Integer.parseInt(br.readLine());
                System.out.println("請輸入修改后的名字:");
                String UserNo = br.readLine();
                System.out.println("請輸入修改后的密碼:");
                String UserTName = br.readLine();
                
                
                sql = "update userlist set UserNo='"+UserNo+"',UserTName='"+UserTName+"'where UserId = '"+id+"'";
                ps.executeUpdate(sql);
                System.out.println("修改成功!");
            }
            //刪除數據
            public void deleteBugu() throws ClassNotFoundException, SQLException, NumberFormatException, IOException{
                String sql = "";
                Connection con1 = MyDBUtil.getConnection();
                PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql);
                InputStreamReader input = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(input);
                System.out.println("請輸入你所要刪除的編號:");
                int id = Integer.parseInt(br.readLine());
                sql = "delete  from userlist where userId='"+id+"'";
                ps.executeUpdate(sql);
            }
            
            


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM