new->project,建立一个“Java Projec..._eclipse jdbc">
赞
踩
访问网址:https://mvnrepository.com
搜索mysql-connector-java
点进去后,选择要下载的版本:
点"jar",保存!
Eclipse中在File->new->project,建立一个“Java Project”工程。
输入工程名称MyJDBC,下一步,点:Open Perspective,然后Finish,即建立了MyJDBC工程
Eclipse中导入方法如下:
IDEA中导入方法如下:
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);
数据库操作有三种方式(注意要定义不同的statement):
Statement stmt = null;//定义静态语句
...
stmt = conn.createStatement();
String Sql = "delete from users where UserId=1 ";
int counts = stmt.executeUpdate(Sql);//有参数
if (counts>0) {
System.out.println("操作成功");
}
PreparedStatement stmt = null;//定义动态语句
...
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("操作成功");
}
CallableStatement stmt = null;//定义存储过程语句
...
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;
直接判断是否成功:
if ( stmt.executeUpdate(Sql)>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.*; 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();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。