目录
四个类
流程
举一个查询类GetCount.java
这个是连接数据库的代码 (需要改成自己的)
static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
static String user = "账号";
static String password = "密码";
package com.sql;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class GetCount {
static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
static String user = "账号";
static String password = "密码";
public static void main(String[] args) {
// TODO Auto-generated method stub
getCount();
}
public static void getCount() {
int count = 0;
try {
// 1. 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.创建链接
Connection connection = (Connection) DriverManager.getConnection(url,user,password);
// 3.创建Statement对象
Statement statement = (Statement) connection.createStatement();
// 4.执行sql
String sql = "select * from student";
ResultSet resultSet = statement.executeQuery(sql);
// 5. 结果
while (resultSet.next()) {
count += 1;
}
if (resultSet !=null) {
resultSet.close();
}
if (statement !=null) {
statement.close();
}
if (connection !=null) {
connection.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(count);
}
}
增删改查的MysqlUtil.java
package com.sql;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class MysqlUtil {
static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
static String user = "账号";
static String password = "密码";
public static void main(String[] args) {
// TODO Auto-generated method stub
delete();
}
/*数据查询
*
*
*/
public static void search() {
try {
Class.forName("com.mysql.jdbc.Driver");
// 2.创建链接
Connection connection = (Connection) DriverManager.getConnection(url,user,password);
// 3.创建Statement对象
Statement statement = (Statement) connection.createStatement();
// 4.执行sql
String sql = "select * from student";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()) {
String id = resultSet.getString("id");
String name = resultSet.getString("name");
String idCard = resultSet.getString("idCard");
String phone = resultSet.getString("phone");
System.out.println(id + " " + name + " " + idCard + " " + phone);
}
if(resultSet!=null) {
resultSet.close();
}
if (statement !=null) {
statement.close();
}
if (connection !=null) {
connection.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*数据插入
*
*
*
* */
private static int insert() {
// TODO Auto-generated method stub
int i =0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection =(Connection) DriverManager.getConnection(url,user,password);
// 创建Statement对象
Statement statement =(Statement) connection.createStatement();
String sql ="insert into student(name,idCard,phone,height) values('苹果','12346','123456','184')";
statement.executeUpdate(sql);
if (statement !=null) {
statement.close();
}
if (connection !=null) {
connection.close();
}
i=1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 1;
}
public static int update() {
int i = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = (Connection) DriverManager.getConnection(url,user,password);
// 3.创建Statement对象
Statement statement = (Statement) connection.createStatement();
String sql = "update student set name = '香蕉' where id = 1" ;
statement.executeUpdate(sql);
if (statement !=null) {
statement.close();
}
if (connection !=null) {
connection.close();
}
i = 1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
public static int delete() {
int i=0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = (Connection) DriverManager.getConnection(url,user,password);
// 3.创建Statement对象
Statement statement = (Statement) connection.createStatement();
String sql = "delete from student where id = 1" ;
statement.executeUpdate(sql);
if (statement !=null) {
statement.close();
}
if (connection !=null) {
connection.close();
}
i = 1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i ;
}
}
在main()中调用
查询:调用search()函数;
添加:调用insert()函数;
更改:调用update()函数;
删除:调用delete()函数;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/115391.html