myeclipse中JDBC連接mysql和簡單的增刪改查
廢話不多說,直接上操作圖,操作起來理解會更徹底
1、myeclipse中項目結構,先在web-inf-lib下導入jar包,註意Web App Libraries這個目錄,下面會有用到的地方
2、連接語句
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
public class BaseDao {
//第一步:先設置四個常量(url數據庫地址,username數據庫用戶名,password數據庫密碼,driver數據庫驅動)
private static final String URL = "jdbc:mysql://127.0.0.1:3306/petshop?characterEncoding=utf-8";
private static final String USERNAME = "root";
private static final String PASSWORD = "ok";
private static final String DRIVER = "com.mysql.jdbc.Driver";//Web App Libraries -- mysql-connector.jar--下的文件名
//1.加載驅動,用靜態塊
//jdbc是java提供給各大數據庫開發商的一些接口(規則),而驅動就是這些規則的具體實現方法
static{
try {
Class.forName(DRIVER);//要求JVM查找並加載指定的類,也就是註冊mysql實現的驅動類
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//2.獲取數據庫連接,通過在DriverManager驅動管理類中設置參數
public Connection getConnection() throws SQLException{
return DriverManager.getConnection(URL,USERNAME,PASSWORD);
}
//3.創建預編譯執行對象PreparedStatement
public PreparedStatement createPreparedStatement(Connection conn,
String sql,Object... params) throws SQLException{
//從當前的conn對象中,獲取執行對象,把預編譯sql語句放入其中
PreparedStatement pstmt = conn.prepareStatement(sql);
//如果預編譯sql中有占位符,則表示要賦值
if(params!=null && params.length>0){
//通過遍歷,將params對象集合中的元素一一對應付給占位符
for(int i=1;i<=params.length;i++){
pstmt.setObject(i, params[i-1]);
}
}
return pstmt;//帶有sql語句的預編譯執行對象
}
//4.創建公共DML執行方法
public int executeDML(String sql,Object... params){
int result = 0;//用於返回受影響的行數來判斷是否執行成功
//創建兩個數據庫對象
Connection conn = null;//數據庫連接對象
PreparedStatement pstmt = null;//預編譯執行對象,接收
try {
conn = this.getConnection();
pstmt = this.createPreparedStatement(conn, sql, params);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
closeAll(conn,pstmt,null);
}
return result;
}
//5.釋放公共資源方法
void closeAll(Connection conn, PreparedStatement pstmt,
ResultSet rs) {
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstmt!=null)
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
3、測試類中的代碼,實現簡單的增刪改查
package utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class TestDML {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("請選擇您要執行的操作\n1.執行 2.刪除 3.修改4.查所有");
TestDML test = new TestDML();
int choose = input.nextInt();
switch (choose) {
case 1://新增
test.insertNewGrade();
break;
case 2://刪除
test.delGrade();
break;
case 3://修改
test.updateGrade();
break;
case 4://查詢
test.selectAll();
break;
}
}
private void selectAll() {
BaseDao bd = new BaseDao();
Connection conn = null;
String sql = "select * from petstore";
PreparedStatement pstmt=null;
ResultSet rs = null;
try {
conn = bd.getConnection();//獲取數據庫鏈接對象
pstmt = bd.createPreparedStatement(conn, sql);
rs = pstmt.executeQuery();
System.out.println("id\tname\tpassword\tbalance");
while(rs.next()){
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getInt(4));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
bd.closeAll(conn,pstmt,rs);
}
}
private void updateGrade() {
System.out.println("請輸入要修改的id編號:");
int Id = input.nextInt();
System.out.println("請輸入新的id:");
int id = input.nextInt();
System.out.println("請輸入新的name:");
String name = input.next();
System.out.println("請輸入新的password:");
String password = input.next();
System.out.println("請輸入新的balance:");
String balance = input.next();
//執行插入語句,需要傳入sql和params
BaseDao bd = new BaseDao();
String sql = "update petstore set id=?, name=?,password=?,balance = ? where id=?";
Object[] params = {id,name,password,balance,Id};
int result = bd.executeDML(sql, params);
if(result>0){
System.out.println("修改成功");
}else{
System.out.println("修改失敗");
}
}
private void delGrade() {
System.out.println("請輸入要刪除的id:");
int id = input.nextInt();
//執行插入語句,需要傳入sql和params
BaseDao bd = new BaseDao();
String sql = "delete from petstore where id=?";
Object[] params = {id};
int result = bd.executeDML(sql, params);
if(result>0){
System.out.println("刪除成功");
}else{
System.out.println("刪除失敗");
}
}
private void insertNewGrade() {
//執行新增(年級編號,年級名稱)
System.out.println("請輸入id:");
int id = input.nextInt();
System.out.println("請輸入name:");
String name = input.next();
System.out.println("請輸入password:");
String password = input.next();
System.out.println("請輸入balance:");
String balance = input.next();
//執行插入語句,需要傳入sql和params
BaseDao bd = new BaseDao();
String sql = "insert into petstore value (?,?,?,?)";
Object[] params = {id,name,password,balance};
int result = bd.executeDML(sql, params);
if(result>0){
System.out.println("新增成功");
}else{
System.out.println("新增失敗");
}
}
}
4、效果,下面是表中原有的一個數據
1、插入
2、刪除
3、更新
4、查看表數據
以上!!!!!
myeclipse中JDBC連接mysql和簡單的增刪改查