标签:jdbc
//创建db.properties文件,来配置对数据库的访问方式 dbname=MySQL /Oracle //通过读取文件来实现对不同的数据库不同访问 import java.io.*; import java.util.*; public class JDBCTestProgrammer1{ public static void main(String[] args) throws Exception{ //通过FileReader读取配置文件 FileReader reader = new FileReader("db.properties"); //创建一个属性对象 Properties prop = new Properties(); //通过属性对象的load方法将reader读取到内存生成一个Map集合 prop.load(reader); //关闭流 reader.close(); //通过属性对象的getProperty(String key) String dbname = prop.getProperty("dbname"); //通过java反射机制创建该类 Class c = Class.forName(dbname); Object obj = c.newInstance(); JDBC jdbc = (JDBC)obj; jdbc.getConnection(); } }
JDBC编程六步骤
1:注册驱动
2:获取数据库连接
3:执行数据库操作对象
4:执行SQL语句
5:处理查询结果集
6:关闭资源
第一步:实现注册驱动
1.1)获取驱动对象
1.2) 注册驱动
import java.sql.Driver; import java.sql.DriverManager; public class DriverTest { public static void main(String[] args){ try{ //获取驱动对象 Driver driver = new com.mysql.jdbc.Driver(); //注册驱动 DriverManager.registerDriver(driver); } catch(Exception e){ e.printStackTrace(); } } }
第二步:获取数据库的连接
import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; public class JDBCTest { public static void main(String[] args){ try{ //1.1获取驱动对象 Driver driver = new com.mysql.jdbc.Driver(); //1.2注册驱动 DriverManager.registerDriver(driver); //2获取数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/test"; String user = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, user, password); System.out.println(conn); } catch(Exception e){ e.printStackTrace(); } } }
第三步:获取数据库的操作对象
import java.sql.Driver; import java.sql.DriverManager; import java.sql.Statement; import java.sql.Connection; public class JDBCTest{ public static void main(String[] args){ try{ //注册驱动 Driver driver = new com.mysql.jdbc.Driver(); DriverManager.registerDriver(driver); //获取数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/test"; String user = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url,user,password); //3:获取数据库操作对象 Statement stmt = conn.createStatement(); System.out.println(stmt); } catch(Exception e){ e.printStackTrace(); } } }
第四步:执行SQL语句
import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCTest{ public static void main(String[] args){ try { //1注册驱动 Driver driver = new com.mysql.jdbc.Driver(); DriverManager.deregisterDriver(driver); //2获取数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/JDBC"; String user = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, user, password); System.out.println(conn); //3获取数据库操作对象 Statement stat = conn.createStatement(); //4执行SQL语句:DQL语句->查询 String sql = "select c_id, c_name , c_zip from customers"; ResultSet rs = stat.executeQuery(sql); System.out.println(rs); } catch (SQLException e) { e.printStackTrace(); } } }
第五步:处理查询结果集
import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCTest{ public static void main(String[] args){ try { //1注册驱动 Driver driver = new com.mysql.jdbc.Driver(); DriverManager.deregisterDriver(driver); //2获取数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/JDBC"; String user = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, user, password); System.out.println(conn); //3获取数据库操作对象 Statement stat = conn.createStatement(); //4执行SQL语句:DQL语句->查询 String sql = "select c_id, c_name , c_zip from customers;"; ResultSet rs = stat.executeQuery(sql); System.out.println(rs); //5处理查询结果 while(rs.next()){ //取数据的第一种方式 String name= rs.getString("c_name"); int id = rs.getInt("c_id"); double zip = rs.getDouble("c_zip"); System.out.println(name+"\t "+id+"\t "+zip); /*不建议以下写法,程序可读性不强 String name= rs.getString(2); int id = rs.getInt(1); double zip = rs.getDouble(3); System.out.println(name+"\t "+id+"\t "+zip); */ } } catch (SQLException e) { e.printStackTrace(); } } }
第六步:关闭资源
由于变量作用域的关系,在try里面定义的变量不能在finally里面读取,所以需要局部变量为全局变量
import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCTest{ public static void main(String[] args){ Connection conn = null; Statement stat = null; ResultSet rs = null; try { //1注册驱动 Driver driver = new com.mysql.jdbc.Driver(); DriverManager.deregisterDriver(driver); //2获取数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/JDBC"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); System.out.println(conn); //3获取数据库操作对象 stat = conn.createStatement(); //4执行SQL语句:DQL语句->查询 String sql = "select c_id, c_name , c_zip from customers;"; rs = stat.executeQuery(sql); System.out.println(rs); //5处理查询结果 while(rs.next()){ //取数据的第一种方式 String name= rs.getString("c_name"); int id = rs.getInt("c_id"); double zip = rs.getDouble("c_zip"); System.out.println(name+"\t "+id+"\t "+zip); /*不建议以下写法,程序可读性不强 String name= rs.getString(2); int id = rs.getInt(1); double zip = rs.getDouble(3); System.out.println(name+"\t "+id+"\t "+zip); */ } } catch (SQLException e) { e.printStackTrace(); } finally{ //6关闭资源 倒叙关闭 if(rs != null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stat != null){ try { stat.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
附:执行DML语句 增删改
import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCTest{ public static void main(String[] args){ Connection conn = null; Statement stat = null; int count = 0; try { Driver driver = new com.mysql.jdbc.Driver(); DriverManager.deregisterDriver(driver); String url = "jdbc:mysql://127.0.0.1:3306/jdbc"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); stat = conn.createStatement(); //执行SQL语句 :DML语句->insert update delect String sql_ins = "insert into t_user(name) values (‘aaa‘)"; count = stat.executeUpdate(sql_ins); String sql_update = "update t_user set name = ‘lll‘ where id = 1"; count = stat.executeUpdate(sql_update); String sql_del = "DELETE FROM t_user where name = ‘aaa‘"; count = stat.executeUpdate(sql_del); System.out.println(count); } catch (SQLException e) { e.printStackTrace(); } } }
本文出自 “逐梦路上的Man” 博客,请务必保留此出处http://meyangyang.blog.51cto.com/12906086/1971378
标签:jdbc
原文地址:http://meyangyang.blog.51cto.com/12906086/1971378