访问网址:https://mvnrepository.com
搜索mysql-connector-java
点进去后,选择要下载的版本:
Eclipse中在File->new->project,建立一个“Java Project”工程。
输入工程名称MyJDBC,下一步,点:Open Perspective,然后Finish
Class.forName(“com.mysql.cj.jdbc.Driver”);
String URL = "jdbc:mysql://localhost:3306/jsp_mysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
String UNAME = "root";
String UPASS = "";
Connection conn = DriverManager.getConnection(URL,UNAME,UPASS);
数据库操作有三种方式:
stmt = conn.createStatement();
String Sql = "delete from users where UserId=1 ";
int counts = stmt.executeUpdate(Sql);//有参数
if (counts>0) {
System.out.println("操作成功");
}
String Sql = "delete from users where UserId=?";
stmt = conn.prepareStatement(Sql);
stmt.setString(1, 1);//设置参数值
//执行SQL,返回受影响的行数
int counts = stmt.executeUpdate();//无参数
if (counts>0) {
System.out.println("操作成功");
}
stmt = conn.prepareCall("{call delUser(?,?,?)}");
//设置参数值
stmt.setInt(1,1);
//执行SQL,返回结果
stmt.execute();//无参数的execute
stmt.registerOutParameter(2, Types.INTEGER);//设置过程调用的输出值类型
int delres = stmt.getInt(2);//取值
stmt.registerOutParameter(3, Types.INTEGER);
int rescounts = stmt.getInt(3);//取值
if (rescounts>0) {
System.out.println("删除记录ID:"+i1+",删除了"+delres+"条,剩余"+rescounts+"条");
}
其中 注:delUser存储过程内容:
delete from users where UserId=p_id;
select ROW_COUNT() into deleteUsers;
select COUNT(UserId) from users into userCounts;
直接判断是否成功:
int rs = stmt.executeUpdate(Sql);
if (rs>0) System.out.println("操作成功");
else
System.out.println(“操作失败");
提取出SQL查询语句返回的结果:
rs = stmt.executeQuery();
while (rs.next()) {
int UId = rs.getInt("UserId");
System.out.println("表记录:"+UId);
}
数据库操作完成,关闭JDBC对象,释放资源
关闭顺序和声明顺序相反:
1)关闭结果集ResultSet对象;
2)关闭Statement/PreparedStatement/CallableStatment对象;
3)关闭连接对象conn
try {
if (rs!=null) rs.close();
if (stmt!=null) stmt.close();
if (conn!=null) conn.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class JDBCTest {
private static final String URL = "jdbc:mysql://localhost:3306/jsp_mysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
private static final String UNAME = "root";
private static final String UPASS = "";
public static void updateDB(){
Connection conn = null; //建立连接
Statement stmt= null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//建立数据库连接
conn = DriverManager.getConnection(URL,UNAME,UPASS);
//建立SQL语句,并执行
stmt = conn.createStatement();
String Sql = "insert into users(UserName,UserPass,UserBirth, UserSubj,UserScores) values "
+ "('王五','',str_to_date('19950102', '%Y%m%d'),'计算机',99)";
//执行SQL,返回受影响的行数
int counts = stmt.executeUpdate(Sql);
if (counts>0) {
System.out.println("数据库操作成功");
}
}catch( ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
//关闭连接和语句
try {
if (stmt!=null) stmt.close();
if (conn!=null) conn.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
public static void main(String[] args) {
updateDB();
}
}
右键run as->java application运行
对于update和delete只需更换Sql语句。
- 查询记录
-返回查询结果的语句:
String Sql = "select * from users";
//执行SQL,返回受影响的记录结果集
rSet = stmt.executeQuery(Sql);
while (rSet.next()) {
int UId = rSet.getInt("UserId");
String Uname = rSet.getString("UserName");
Date Udate = rSet.getDate("UserBirth");
System.out.println("数据库记录结果:");
System.out.println("UserId:"+UId );
System.out.println("UserName:"+Uname);
System.out.println("UserBirthday:"+Udate);
}
修改静态SQL程序,使用如下语句,即可完成动态SQL:
- 增删改操作
stmt = conn.prepareStatement(Sql);
//设置参数值
stmt.setString(1, "XX");
stmt.setDate(2,XX);
stmt.setInt(3, XX);
int counts = stmt.executeUpdate();//无参数的execute
- 查询
String Sql = "select * from users where UserName like ?";
stmt = conn.prepareStatement(Sql);
stmt.setString(1, "%一%");
//执行SQL,返回记录结果集
rs = stmt.executeQuery();