入门
基本概念祥解各个对象1.DriverManager2.Connetion3.Statement4.ResultSet对象ResulSet使用
JDBCUtils简易
基本概念
其实是一套接口。每个数据库的厂商需要去实现这个接口,提供数据库jar包。
导入jar包 下载地址(https://dev.mysql.com/downloads/connector/j/)注册驱动获取数据库来凝结对象 Connection定义sql获取执行sql的对象Statement执行sql,获得结果处理结果释放资源
import java
.sql
.Connection
;
import java
.sql
.DriverManager
;
import java
.sql
.SQLException
;
import java
.sql
.Statement
;
public class JDBCdemo2 {
public static void main(String
[] args
) {
Statement stmt
= null
;
Connection conn
= null
;
try {
Class
.forName("com.mysql.cj.jdbc.Driver");
String sql
= "insert into student values('cwm',29,99,null)";
conn
= DriverManager
.getConnection("jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC","root","123456");
stmt
= conn
.createStatement();
int count
= stmt
.executeUpdate(sql
);
System
.out
.println(count
);
} catch (Exception e
) {
e
.printStackTrace();
}finally {
if (stmt
!=null
){
try {
stmt
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if (conn
!=null
){
try {
conn
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
}
}
祥解各个对象
1.DriverManager
功能: 注册驱动 ;Class.forName(“com.mysql.cj.jdbc.Driver”); 获取连接对象。
2.Connetion
获取执行sql的对象 Statement createStatemente() PreparedStatement PreparedStatement(String sql)管理事务 开启事务 setAutoCommit(boolean autoCommit):设置该参数为false 即开启事务 提交事务 commit() 回滚事务 rollback
3.Statement
用于执行静态sql并返回生成的结果的对象
执行sql int executeUpdate(String sql) : 执行DML(增删改)语句、DDL(create,alter,drop)语句,返回影响的行数。 ResultSet executeQuery(String sql): 执行select语句
4.ResultSet对象
封装执行结果集对象
boolean next():光标移到下一行 getInt(参数) getString(参数) 参数 Int:第n列 String :列的名称
ResulSet使用
import domain
.Stu
;
import java
.sql
.*
;
import java
.util
.ArrayList
;
import java
.util
.List
;
public class JDBCdemo4 {
public List
<Stu> findAll() {
Statement stmt
= null
;
Connection conn
= null
;
ResultSet rs
= null
;
String sql
= "select * from student";
Stu stu
= null
;
List
<Stu> list
= new ArrayList<>();
try {
conn
= JDBCUtils
.getConnetion();
stmt
= conn
.createStatement();
rs
= stmt
.executeQuery(sql
);
while (rs
.next()) {
String name
= rs
.getString("name");
int age
= rs
.getInt("age");
double score
= rs
.getDouble("score");
int id
= rs
.getInt("id");
stu
= new Stu();
stu
.setAge(age
);
stu
.setId(id
);
stu
.setScore(score
);
stu
.setName(name
);
list
.add(stu
);
}
return list
;
}catch (SQLException e
) {
e
.printStackTrace();
} finally {
if (rs
!= null
) {
try {
rs
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if (stmt
!= null
) {
try {
stmt
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if (conn
!= null
) {
try {
conn
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
return null
;
}
public static void main(String
[] args
) {
List
<Stu> list
= new JDBCdemo3().findAll();
System
.out
.println(list
);
}
}
JDBCUtils简易
import java
.io
.FileReader
;
import java
.io
.IOException
;
import java
.net
.URL
;
import java
.sql
.*
;
import java
.util
.Properties
;
public class JDBCUtils {
private static String url
;
private static String user
;
private static String password
;
private static String driver
;
static {
try {
Properties po
=new Properties();
ClassLoader classLoader
= JDBCUtils
.class.getClassLoader();
URL res
= classLoader
.getResource("jdbc.properties");
String path
=res
.getPath();
System
.out
.println(path
);
po
.load(new FileReader("src/jdbc.properties"));
url
= po
.getProperty("url");
user
= po
.getProperty("user");
password
= po
.getProperty("password");
driver
= po
.getProperty("driver");
Class
.forName(driver
);
} catch (IOException e
) {
e
.printStackTrace();
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
}
}
public static Connection
getConnetion() throws SQLException
{
return DriverManager
.getConnection(url
,user
,password
);
}
public static void close(Statement stmt
,Connection conn
){
if (stmt
!= null
) {
try {
stmt
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if (conn
!= null
) {
try {
conn
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
public static void close(ResultSet rs
,Statement stmt
, Connection conn
){
if (rs
!= null
) {
try {
rs
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if (stmt
!= null
) {
try {
stmt
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if (conn
!= null
) {
try {
conn
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
}
配置文件如下:
url
=jdbc
:mysql
:/
user
=root
password
=你的密码
driver
=com
.mysql
.cj
.jdbc
.Driver
未完待续。。。。