Last active
November 20, 2019 05:59
-
-
Save MrFant/20a15fc3f70236ed51fcba3fba037aee to your computer and use it in GitHub Desktop.
仅用jdbc连接数据库的demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 依赖jdbc | |
import java.sql.*; | |
public class jdbc{ | |
public static void main(String[] args) throws Exception{ | |
//1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动 | |
Class.forName("com.mysql.jdbc.Driver");//硬编码 | |
/* | |
* 2、通过驱动管理者获取数据库连接 | |
* DriverManager是驱动实现类的管理者。 | |
* url:连接数据库的位置,端口号,数据库名 | |
* jdbc:mysql://localhost:3306/test_01 | |
*/ | |
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_1", "root", "root");//(硬编码) | |
/* | |
* 3、获得sql语句执行者 | |
* statement:执行sql语句对象 | |
* sql语句必须是完整的。 | |
* preparedStatement:预处理(常用) | |
* sql语句可以不是完整的,可以将参数用?替代,然后在预编译后加入未知参数 | |
* | |
*/ | |
//定义sql语句,?表示占位符 | |
String sql = "select * from user where id = ?"; //硬编码 | |
PreparedStatement ps = conn.prepareStatement(sql); | |
// 设置第一个参数值为int类型的1,后面那个1是index,从1开始,不是从0开始。 | |
ps.setInt(1, 1); //硬编码 | |
//4、获取sql语句执行结果,resultset | |
/* | |
* executeQuery():查询操作 | |
* executeUpdate():增删改操作 | |
*/ | |
ResultSet rs= ps.executeQuery(); | |
//5、处理结果 | |
while(rs.next()){ | |
System.out.println(rs.getInt(1));;//index代表第几列,从1开始 | |
} | |
//6、关闭连接 | |
rs.close(); | |
ps.close(); | |
conn.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
public class jdbc{ | |
public static void main(String[] args) throws Exception { | |
//1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动 | |
Class.forName("com.mysql.jdbc.Driver");//硬编码 | |
/* | |
* 2、通过驱动管理者获取数据库连接 | |
* DriverManager是驱动实现类的管理者。 | |
* url:连接数据库的位置,端口号,数据库名 | |
* jdbc:mysql://localhost:3306/test_01 | |
*/ | |
try(Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis", "root", "root")){ | |
/* | |
* 3、获得sql语句执行者 | |
* statement:执行sql语句对象 | |
* sql语句必须是完整的。 | |
* preparedStatement:预处理(常用) | |
* sql语句可以不是完整的,可以将参数用?替代,然后在预编译后加入未知参数 | |
* | |
*/ | |
//定义sql语句,?表示占位符 | |
String sql = "select * from student where id = ?"; //硬编码 | |
try(PreparedStatement ps = conn.prepareStatement(sql)){ | |
ps.setInt(1, 1); //硬编码 | |
//4、获取sql语句执行结果,resultset | |
/* | |
* executeQuery():查询操作 | |
* executeUpdate():增删改操作 | |
*/ | |
try(ResultSet rs= ps.executeQuery()){ | |
//5、处理结果 | |
while(rs.next()){ | |
System.out.println(rs.getString(3));;//index代表第几列,从1开始 | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
第一个jdbc.java没有把关闭连接的语句放在finally里面,不是好的实践,jdbc2.java使用了java7 的twr语法,可以在try里面安全关闭打开的资源