來(lái)源: whh743 發(fā)布時(shí)間:2018-11-21 14:09:32 閱讀量:1001
這里我們使用mysql數(shù)據(jù)庫(kù)
1:創(chuàng)建一個(gè)測(cè)試用的表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8
2:創(chuàng)建一個(gè)user表的實(shí)體類,并加上get set方法
public class User {
private int id;
private String name;
private String sex;
private int age;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3:添加獲取數(shù)據(jù)庫(kù)連接,增加,刪除,修改,更新,測(cè)試的方法
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.PreparedStatement;
public class JdbcUtils {
// 獲取數(shù)據(jù)庫(kù)的連接
public static Connection getConnection() {
String driver = "com.mysql.jdbc.Driver";// 驅(qū)動(dòng),固定寫法
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";// 不加字符編碼中文會(huì)亂碼,親測(cè)
String userName = "admin";
String password = "7758258";
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, userName, password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 增加
public static void getAllUser() {
Connection conn = JdbcUtils.getConnection();
String sql = "select * from user";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + "\t");
}
System.out.println("");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 插入
public static int insertUser(User user) {
Connection conn = JdbcUtils.getConnection();
int i = 0;
String sql = "insert into user (name,sex,age) values(?,?,?)";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getSex());
pstmt.setInt(3, user.getAge());
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
// 修改
public static int updateUser(User user) {
Connection conn = JdbcUtils.getConnection();
int i = 0;
String sql = "update user set age='" + user.getAge() + "' where name='"
+ user.getName() + "'";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
// 刪除
public static int deleteUser(User user) {
Connection conn = JdbcUtils.getConnection();
int i = 0;
String sql = "delete from user where name='" + user.getName() + "'";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
// 用于測(cè)試的main方法
public static void main(String[] args) {
// 測(cè)試查詢方法
JdbcUtils.getAllUser();
// 測(cè)試插入方法
User user = new User();
user.setName("張三");
user.setSex("男");
user.setAge(55);
int i = JdbcUtils.insertUser(user);
System.out.print(i);
// 測(cè)試更新方法
user.setAge(100);
user.setName("zhangsan");
i = JdbcUtils.updateUser(user);
//測(cè)試刪除方法
user.setName("zhangsan");
i = JdbcUtils.deleteUser(user);
}
}
JAR包的配置,我們使用的是maven工程。在pom.xml引入
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.1</version>
</dependency>
---------------------
在線
客服
服務(wù)時(shí)間:周一至周日 08:30-18:00
選擇下列產(chǎn)品馬上在線溝通:
客服
熱線
7*24小時(shí)客服服務(wù)熱線
關(guān)注
微信
關(guān)注官方微信