#db mysql#jdbc.driver=com.mysql.jdbc.Driver#jdbc.url=jdbc:mysql://localhost:3306/mysql?&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull#jdbc.username=root#jdbc.password=#在xml配置文件中,url中的&符号需要转义成&。比如在tomcat的server.xml中配置数据库连接池时,mysql jdbc url样例如下#jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=utf-8#db oracle#jdbc.driver=oracle.jdbc.driver.OracleDriver#jdbc.url="jdbc:oracle:thin:@192.168.8.1:1521:orcl#jdbc.username=root#jdbc.password=#http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html#db postgressjdbc.driver=org.postgresql.Driverjdbc.url=jdbc:postgresql://localhost/softjdbc.username=postgresjdbc.password=postgres#https://jdbc.postgresql.org/download.html
package com.sdzw.wgn;import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Connection; import java.sql.Statement; public class JDBC_Test { public void test() { Connection conn = null; // MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值 // 避免中文乱码要指定useUnicode和characterEncoding String url = "jdbc:mysql://localhost:3306/test?" + "user=root&password=123456&useUnicode=true&characterEncoding=UTF8"; try { // 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来, // 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以 Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动 // or: // com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver(); // or: // new com.mysql.jdbc.Driver(); System.out.println("成功加载MySQL驱动程序"); // 一个Connection代表一个数据库连接 conn = DriverManager.getConnection(url); // Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等 Statement stmt = conn.createStatement(); String sql = "create table student(NO char(20),name varchar(20),primary key(NO))"; int result = stmt.executeUpdate(sql);// executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功 if (result != -1) { System.out.println("创建数据表成功"); sql = "insert into student(NO,name) values('2016001','刘大')"; result = stmt.executeUpdate(sql); sql = "insert into student(NO,name) values('2016002','陈二')"; result = stmt.executeUpdate(sql); sql = "select * from student"; ResultSet rs = stmt.executeQuery(sql);// executeQuery会返回结果的集合,否则返回空值 System.out.println("学号\t姓名"); while (rs.next()) { System.out.println(rs.getString(1)+ "\t" + rs.getString(2));// 入如果返回的是int类型可以用getInt() } } } catch(SQLException e) { System.out.println("MySQL操作错误"); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { }}
public void getJDBCPros(String path) throws IOException { Properties properties = new Properties(); InputStream in = new BufferedInputStream(new FileInputStream(path)); properties.load(in); String driver = properties.getProperty("jdbc.driver"); String url = properties.getProperty("jdbc.url"); String username = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); }