本文共 6263 字,大约阅读时间需要 20 分钟。
//注册驱动
//方法一:Driver driver = new com.mysql.jdbc.Driver();DriverManager.registerDriver(driver);
//方法二:常用
Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","1127");System.out.println(conn);//方法一:com.mysql.jdbc.JDBC4Connection@66d1af89 //方法二:com.mysql.jdbc.JDBC4Connection@4a87761d
举例1:
package loey.java;import java.sql.*;public class JDBCTest01 { public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ // 1、注册驱动 Driver driver = new com.mysql.jdbc.Driver();//多态,父类型引用指向子类型对象 DriverManager.registerDriver(driver); // 2、获取连接 /* url包括哪几部分: 协议 IP Port 资源名 eg:http://180.101.49.11:80/index.html http:// 通信协议 180.101.49.11 IP地址 80 端口号 index.html 资源名 */ // static Connection getConnection(String url, String user, String password) String url = "jdbc:mysql://127.0.0.1:3306/bjpowernode"; String user = "root"; String password = "1127"; conn = DriverManager.getConnection(url,user,password); //System.out.println("数据库连接对象" + conn);//数据库连接对象com.mysql.jdbc.JDBC4Connection@4eb7f003 // 3、获取数据库操作对象 // Statement createStatement() 创建一个 Statement 对象来将 SQL 语句发送到数据库。 stmt = conn.createStatement(); // 4、执行sql语句 // int executeUpdate(String sql) // 专门执行DML语句 // 返回值是“影响数据库中的记录条数” String sql = "update emp1 set job = '普通职员',sal = 900 where ename = 'SMITH'"; int count = stmt.executeUpdate(sql); System.out.println(count == 1 ? "修改成功" : "修改失败"); // 5、处理查询结果集 }catch(SQLException e){ e.printStackTrace(); }finally{ // 6、释放资源 // 从小到大依次关闭 if(conn != null){ try{ conn.close(); }catch(Exception e){ e.printStackTrace(); } } if(stmt != null){ try{ stmt.close(); }catch(Exception e){ e.printStackTrace(); } } } }}
package loey.java;/* 使用资源绑定器,jdbc.properties*/import java.sql.*;import java.util.*;import java.io.*;public class JDBCTest04 { public static void main(String[] args) { //方法一 //ResourceBundle bundle = ResourceBundle.getBundle("jdbc"); //String driver = bundle.getString("driver"); //String url = bundle.getString("url"); //String user = bundle.getString("user"); //String password = bundle.getString("password"); //String sql = bundle.getString("sql"); //方法二: Properties pros = new Properties(); InputStream is = null; //读取配置文件的方式一:// FileInputStream fis = new FileInputStream("jdbc.properties");// pros.load(fis); //读取配置文件的方式二:使用ClassLoader //配置文件默认识别为:当前module的src下// 关于类加载器的一个主要方法:getResourceAsStream(String str):获取类路 径下的指定文件的输入流 try{ ClassLoader classLoader = JDBCTest04.class.getClassLoader(); is = classLoader.getResourceAsStream("jdbc.properties"); pros.load(is); }catch(IOException e){ e.printStackTrace(); } String driver = pros.getProperty("driver"); String url = pros.getProperty("url"); String user = pros.getProperty("user"); String password = pros.getProperty("password"); Connection conn = null; Statement stmt = null; try{ Class.forName(driver); conn = DriverManager.getConnection(url,user,password); stmt = conn.createStatement(); String sql = "insert into emp1(empno,ename,job,sal) values(30,'张','销售员',1500)"; int count = stmt.executeUpdate(sql); System.out.println(count == 1 ? "插入成功" : "插入失败"); }catch(Exception e){ e.printStackTrace(); }finally{ if(stmt != null){ try{ stmt.close(); }catch(Exception e){ e.printStackTrace(); } } if(is != null){ try{ is.close(); }catch(Exception e){ e.printStackTrace(); } } } }}
package loey.DBUtil;import org.junit.Test;import java.io.IOException;import java.io.InputStream;import java.sql.*;import java.util.Properties;/** * JDBC工具类,简化JDBC编程 */public class DBUtil { /** * 工具类中的构造方法是私有的 * 因为工具类中的方法都是静态的,直接通过类名去调即可。 */ private DBUtil() { } private static Properties getProperties(){ //ResourceBundle bundle = ResourceBundle.getBundle("jdbc"); //String driver = bundle.getString("driver"); //String url = bundle.getString("url"); //String user = bundle.getString("user"); //String password = bundle.getString("password"); //String sql = bundle.getString("sql"); // 1.读取配置文件中的4个基本信息 Properties pros = new Properties(); InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties"); try { pros.load(is); } catch (IOException e) { e.printStackTrace(); } return pros; } /** * 静态代码块,类加载的时候执行 * 把注册驱动程序的代码放在静态代码块中,避免多次获取连接对象时重复调用 */ static{ Properties pros = getProperties(); String driver = pros.getProperty("driver"); try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * @return 获取连接 * @throws SQLException */ public static Connection getConnection(String database) throws Exception { Properties pros = getProperties(); String url = pros.getProperty("url"); String user = pros.getProperty("user"); String password = pros.getProperty("password"); Connection conn = DriverManager.getConnection(url + database, user, password); return conn; } public static void close(Connection conn, Statement ps, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }}
jdbc.properties
注意:该文件要在src下driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/user=rootpassword=1127
转载地址:http://lxuki.baihongyu.com/