java與數據庫的連接
1.JDBC簡介
1.JDBC(Java Data Base Connectivity)是由java編程語言編寫的類及接口組成,
利用JDBC可以將JAVA代碼連接到oracle、DB2、SQLServer、MYSQL等數據庫
從而實現對數據庫中的數據操作的目的
2.JDBC是sun公司開發的一套數據庫訪問編程接口
3.JDBC四個核心類
* DriverManager 創建連接
* Connection 連接類
* Statement 執行SQL語句-->執行查詢指令(executeQuery)
* ResultSet 結果集
2.JDBC連接步驟
* JDBC連接步驟
* 1.註冊驅動
* 2.獲取連接
* 3.獲取SQL語句的執行對象 Statement
* 4.執行SQL語句返回結果集 ResultSet
* 5.處理結果集
* 6.關閉資源
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
public class text1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.註冊一個驅動
Class.forName("com.mysql.jdbc.Driver")
//2.獲取連接對象
String url = "jdbc:mysql://localhost:3306/myjdbc"
//3.連接數據庫(是個對象)
Connection connection = DriverManager.getConnection(url, "root", "123456")
//4.獲取執行SQL語句的對象
Statement statement = connection.createStatement()
//5.執行sql語句 返回結果集
String sql = "select * from users"
ResultSet resultSet = statement.executeQuery(sql)
//6.處理結果集-->輸出SQL語句讀取結果
while(resultSet.next()) { //next()讀行指針
System.out.println(resultSet.getObject(1))
System.out.println(resultSet.getObject(2))
System.out.println(resultSet.getObject(3))
System.out.println(resultSet.getObject(4))
System.out.println(resultSet.getObject(5))
//注意:查詢數據庫時 索引是從1開始的
}
//7.關閉資源
resultSet.close()
statement.close()
connection.close()
}
}
3.JDBC增刪改查
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.jupiter.api.Test;
public class text2 {
-------------------1.增刪方法
@Test
public void test() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/myjdbc?"
+ "user=root&password=123456";
Connection connection = DriverManager.getConnection(url);
Statement statement = (Statement) connection.createStatement();
String sql = "insert into users values"
+ "(5,'gl','123','[email protected]','2000-01-03')";
int row = statement.executeUpdate(sql);
System.out.println(row);
if (row > 0) {
System.out.println("插入成功");
}
statement.close();
connection.close();
}
---------------------------------------------------------------------------------------
2.查詢方法
@Test
public void testSelect()throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/myjdbc?"
+ "user=root&password=123456";
Connection connection = DriverManager.getConnection(url);
Statement statement = (Statement) connection.createStatement();
String sql = "select id,name,email from users";
ResultSet resultSet = statement.executeQuery(sql);
System.out.println(resultSet);
while(resultSet.next()) {
System.out.println(resultSet.getObject("id"));
System.out.println(resultSet.getObject("name"));
System.out.println(resultSet.getObject("email"));
}
resultSet.close();
statement.close();
connection.close();
}
}
2.使用對象類打印查詢結果
2.1創建User類
package com.lanou3g.demo;
import java.sql.Date;
public class User {
private int id;
public User() {
super();
}
public User(int id, String name, String password, String email, Date birthday) {
super();
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.birthday = birthday;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + ", email=" + email + ", birthday="
+ birthday + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
private String name;
private String password;
private String email;
private Date birthday;
}
2.2創建mysql查詢類
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class DemoException {
public static void main(String[] args) {
ResultSet resultSet = null;
Statement statement = null;
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/myjdbc";
try {
connection = DriverManager.getConnection(url, "root", "123456");
statement = connection.createStatement();
String sql = "select * from users";
resultSet = statement.executeQuery(sql);
ArrayList<User> list = new ArrayList<>();
while(resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
user.setBirthday(resultSet.getDate("birthday"));
user.setEmail(resultSet.getString("email"));
list.add(user);
}
for(User user : list) {
System.out.println(user);
}
} catch (SQLException e) {
throw new RuntimeException("獲取連接失敗");
}finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception e2) {
throw new RuntimeException("關閉失敗");
}
resultSet = null;
}
if (statement != null) {
try {
statement.close();
} catch (Exception e2) {
throw new RuntimeException("關閉失敗");
}
statement = null;
}
if (connection != null) {
try {
connection.close();
} catch (Exception e2) {
throw new RuntimeException("關閉失敗");
}
connection = null;
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("驅動加載失敗");
}
}
}
幅度太長下篇繼續