1. 程式人生 > >純Java實現控制檯對資料庫的增刪改查(Eclipse)

純Java實現控制檯對資料庫的增刪改查(Eclipse)

package com.hznu.qjc.daos;

import com.hznu.qjc.users.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DAO {
private static Connection getConn() {    
Connection conn = null;
try {
       Class.forName("com.mysql.jdbc.Driver"); //classLoader,載入對應驅動      
   } catch (ClassNotFoundException e) {
       e.printStackTrace();
   }
   String url = "jdbc:mysql://localhost:3306/db_database01";
   String username = "root";//資料庫賬戶,一般為root
   String password = "";//資料庫密碼
   
   try{
       conn = DriverManager.getConnection(url, username, password);
   } catch (SQLException e) {
   e.printStackTrace();
   }
   return conn;
}

public static int insert(Student student) {
   Connection conn = getConn();
   int i = 0;
   String sql = "insert into students (Name,Sex,Age) values(?,?,?)";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement) conn.prepareStatement(sql);
       pstmt.setString(1, student.getName());
       pstmt.setString(2, student.getSex());
       pstmt.setString(3, student.getAge());
       i = pstmt.executeUpdate();
       pstmt.close();
       conn.close();
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return i;
}

public static int update(Student student) {
   Connection conn = getConn();
   int i = 0;
   String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement) conn.prepareStatement(sql);
       i = pstmt.executeUpdate();
       System.out.println("resutl: " + i);
       pstmt.close();
       conn.close();
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return i;
}

public static Integer getAll() {
   Connection conn = getConn();
   String sql = "select * from students";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement)conn.prepareStatement(sql);
       ResultSet rs = pstmt.executeQuery();
       int col = rs.getMetaData().getColumnCount();
       System.out.println("============================");
       while (rs.next()) {
           for (int i = 1; i <= col; i++) {
               System.out.print(rs.getString(i) + "\t");
               if ((i == 2) && (rs.getString(i).length() < 8)) {
                   System.out.print("\t");
               }
            }
           System.out.println("");
       }
           System.out.println("============================");
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return null;
}

public static int delete(String name) {
   Connection conn = getConn();
   int i = 0;
   String sql = "delete from students where Name='" + name + "'";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement) conn.prepareStatement(sql);
       i = pstmt.executeUpdate();
       System.out.println("resutl: " + i);
       pstmt.close();
       conn.close();
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return i;
}
}