數據庫連接,實現增刪改查操作具體步驟(全)


1.連接數據庫必要的步驟:

jdbc:

driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/lxn
user=root
password=lxn123

oracle:

#driver=oracle.jdbc.driver.OricerDriver
#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
#user=system
#password=lxn123

2.連接數據庫的兩種方法,實現數據庫的增刪改查

package com.lanqiao.javatest;


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Properties;

import org.junit.Test;

import com.mysql.jdbc.Statement;

 

 

public class TestDriverManager {
/*
* 連接數據庫的方法:
* 1.使用DriverManager連接數據庫
* 2.Driver連接:是一個接口,是一個本地實現的接口,能從其中獲取數據庫的驅動的鏈接
* */

//方法一:DriverManager方法,連接數據庫

public void testDriverManager() throws Exception{
//連接數據庫的四個要素:
String driverClass=null;
String jdbcUrl=null;
String user=null;
String password=null;

//讀取類路徑文件下的jdbc.properties
InputStream in=
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);//從輸入流中讀取文件

//依次獲取properties中的文件
driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("user");
password=properties.getProperty("password");

Class.forName(driverClass);//反射機制獲取文件中的driverClass

//通過driverManager中的getConnection方法獲取數據的連接
Connection conn=DriverManager.getConnection(jdbcUrl,user,password);
System.out.println(conn);
}

//方法二:使用driver接口連接數據庫
//1.
public Connection getConnection() throws Exception{
//對數據庫的連接使用者個,里面有Connection接口,實現數據的連接,並可以進行數據庫的增刪改查的操作
//連接數據里的四個必要步驟:
String driverClass=null;
String jdbcUrl=null;
String user=null;
String password=null;

//讀取類路徑文件下的jdbc.properties文件
InputStream in=getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
//從輸入流中讀取文件
properties.load(in);

//依次獲取文件中的內容
driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("user");
password=properties.getProperty("password");

//反射機制獲取
@SuppressWarnings("unused")
Driver driver=(Driver)Class.forName(driverClass).newInstance();//獲取運行時類的方法及屬性

Properties info=new Properties();
info.put("user", "root");
info.put("password", "lxn123");
Connection conn=driver.connect(jdbcUrl, info);
return conn;
}

//測試testDriver()方法;
public void testDriver1() throws Exception{
System.out.println(getConnection());
}
/*
* 1.連接數據庫之后對指定的數據庫中的增加,刪除,更改,查詢的操作
* statement用於執行sql語句的對象;
* 通過Connection的stateStatement()方法來獲取
* 同過executeUpdate(sql)可以執行sql語句
* 傳入的sql語句可以是insect(插入),update(修改),delete(刪除),但是不可以是select(查詢)
* 2.需要注意的是:Connection,statement都是應用程序和數據庫服務器的鏈接資源,使用后都要關閉;
* */

public void testStatement() throws Exception{//數據庫的插入,刪除,更改,查詢需要的步驟;
//1.獲取數據庫的
Connection connn=getConnection();
//2.准備插入的sql語句

//將sql語句插入到在數據庫中建立的表格中
//4.進行插入,刪除,更改,查詢的操作

//插入
String sql1="INSERT INTO CUSTOMER(NAME,EMALL,BIRTH)VALUES('pa','LJ','1932-2-12');";
//刪除
String sql2="delete from customer where id=27";
//修改
String sql3="update customer set name='jiajia' where id=2564";
//獲取sql語句的statement對象,調用statement對象的executeUpdate(sql),執行sql語句的插入
Statement statement=(Statement) connn.createStatement();
statement.executeUpdate(sql1);
statement.executeUpdate(sql2);
statement.executeUpdate(sql3);
//5關閉statement對象。
statement.close();
//3.關閉連接
connn.close();
}
//sql為數據庫語句,這個為數據庫的增刪改的方法
public void testStatement1(String sql){
Connection conn=null;
Statement sta=null;
try {
conn=getConnection();
sta=(Statement) conn.createStatement();
sta.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(sta!=null){
try {
sta.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//數據庫的查詢方法
@Test
public void testResultSet(){
//獲取指定的id,或name的屬性記錄值,並打印
Connection connection=null;
Statement statement=null;
ResultSet resultset=null;

try {
//1.獲取Connection
connection=getConnection();

//2.獲取Statement
statement=(Statement)connection.createStatement();

//3.准備sql語句
String sql="select id,name,email,birth from customer";

//4.執行查詢,得到ResultSet
resultset=statement.executeQuery(sql);

//5.處置ResultSet
while(resultset.next()){
int id=resultset.getInt(1);
String name=resultset.getString(2);
String email=resultset.getString(3);
Date birth=resultset.getDate(4);
System.out.println(id+"--"+name+"--"+email+"--"+birth);
}

} catch (Exception e) {
// TODO: handle exception
}
//6.關閉數據庫資源
finally{
release(connection,statement,resultset);
}
}

//關閉數據庫資源的方法
public static void release(Connection conn,Statement sta,ResultSet rs){
if(conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(sta!=null){
try {
sta.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


免責聲明!

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



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