文章目录
整体框架步骤代码分析
整体框架
步骤
导入jar包(mysql)-mysql-connector-java-5.1.44-bin-jar创建DBLink.java文件连接数据库
package jbdc
;
import java
.sql
.Connection
;
import java
.sql
.DriverManager
;
import java
.sql
.SQLException
;
import java
.sql
.Statement
;
public class DBLink {
public boolean update(String sql
) {
Connection connection
= null
;
Statement statement
= null
;
try {
Class
.forName("com.mysql.jdbc.Driver");
String url
="jdbc:mysql://127.0.0.1:3306/test?useSSL=false";
connection
= DriverManager
.getConnection(url
, "root", "root");
statement
= connection
.createStatement();
int result
= statement
.executeUpdate(sql
);
return result
>0;
} catch (Exception e
) {
e
.printStackTrace();
}finally {
try {
if(statement
!=null
) {
statement
.close();
}
} catch (SQLException e
) {
e
.printStackTrace();
}
try {
if(connection
!=null
) {
connection
.close();
}
} catch (SQLException e
) {
e
.printStackTrace();
}
}
return false;
}
}
代码分析
Class.forName(“com.mysql.jdbc.Driver”); 作用是加载驱动-文件名在Referenced Libraries——com.mysql——jdbc——Driver.class(右键复制Copy Qualified Name)String url=“jdbc:mysql://127.0.0.1:3306/test?useSSL=false”;作用指定连接哪一台计算机上的哪个数据库-这里我的电脑不加后面的?useSSL=false,连接会出错,在网上找到了解决方法,可以看我以前的文章。读者可根据自己的操作看看是否需要加上。connection = DriverManager.getConnection(url, “root”, “root”);作用获取数据库连接对象,一个对象表示一次连接statement = connection.createStatement();//获取Statement对象int result = statement.executeUpdate(sql);//执行sql语句,返回受影响的行数,仅限于数据insert,update,delete不包括查询特别说明-之所以在需要加上finally,是保证资源得到释放而不被各种可能会出现的异常导致资源无法释放。而在finally中加上if语句,则是保证了不会出现空指针异常。(若没有if语句,statement,connection可能会因为异常导致没有赋值成功,那它们的值就有可能是nnull,null去调用close方法时,就会出现空指针异常。)
package jbdc
;
import java
.sql
.Connection
;
import java
.sql
.DriverManager
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.sql
.Statement
;
public class Test {
public static void delete() {
String sql
= "delete from user_info";
if(new DBLink().update(sql
)) {
System
.out
.println("删除成功");
return;
}
System
.out
.println("删除失败");
}
public static void insert() {
String sql
= "insert into user_info (id,name,mobile,address) values('4ce96822-466e-4556-a739-2c4fc6d1cc78','水蜜桃','2349248924','四川成都'";
if(new DBLink().update(sql
)) {
System
.out
.println("添加成功");
return;
}
System
.out
.println("添加失败");
}
public static void update() {
String sql
= "update user_info set name = 'Jimmy',mobile='1234567890'where id='4ce96822-466e-4556-a739-2c4fc6d1cc78'";
if(new DBLink().update(sql
)) {
System
.out
.println("修改成功");
return;
}
System
.out
.println("修改失败");
}
public static void select() {
Connection connection
= null
;
Statement statement
=null
;
ResultSet resultSet
=null
;
try {
Class
.forName("com.mysql.jdbc.Driver");
String url
= "jdbc:mysql://127.0.0.1:3306/test?useSSL=false";
connection
= DriverManager
.getConnection(url
, "root", "root");
statement
= connection
.createStatement();
resultSet
= statement
.executeQuery("select id,name,mobile,address from user_info");
while(resultSet
.next()) {
String id
= resultSet
.getString("id");
String name
= resultSet
.getString("name");
String mobile
= resultSet
.getString("mobile");
String address
= resultSet
.getString("address");
System
.out
.println(id
+","+name
+","+mobile
+","+address
);
}
} catch (Exception e
) {
e
.printStackTrace();
}finally {
try {
if(resultSet
!=null
) {
resultSet
.close();
}
} catch (SQLException e
) {
e
.printStackTrace();
}
try {
if(statement
!=null
) {
statement
.close();
}
} catch (SQLException e
) {
e
.printStackTrace();
}
try {
if(statement
!=null
) {
connection
.close();
}
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
public static void main(String
[] args
) {
select();
}
}