从操作配置文件properties中读取连接字符串,通过该字符串进行数据连接,需要写三个文件其中,两个是java类,一个是后缀名为.properties的文件,该文件放在src工作目录下。
需要准备的包:
https://github.com/chenhaoxiang/Java
后缀为.properties的文件此处为其取名为jdbc.properties,其中的代码如下:
##MySQL driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8 username=root password=1234 ##Oracle #driver=oracle.jdbc.driver.OracleDriver #url=jdbc:oracle:thin:@localhost:1521:orcl #username=scott #password=tiger
可以配置多个数据库代码,形成框架,这里只配置了MySQL和Oracle数据库的、如果换数据库了,就把相应数据库的#号注释去掉,把原来的代码给用#号注释就可以了。
接下来用单例写一个Connection的工厂类ConnFactory.java:
package cn.hncu.util; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class ConnFactory { private static Connection con = null; //静态块 static{ try { //读取配置文件 Properties p = new Properties(); p.load(ConnFactory.class.getClassLoader().getResourceAsStream("jdbc.properties")); String drive = p.getProperty("driver"); String url = p.getProperty("url"); String user = p.getProperty("username"); String password = p.getProperty("password"); Class.forName(drive); con = DriverManager.getConnection(url, user, password); } catch (IOException e) { throw new RuntimeException("配置文件出现异常", e); } catch (ClassNotFoundException e) { throw new RuntimeException("Drive.Class文件出现异常", e); } catch (SQLException e) { throw new RuntimeException("数据库访问出现异常", e); } } public static Connection getConnection(){ return con; } public static void main(String[] args) { System.out.println(getConnection()); } }
演示Statement中的4个执行方法:
1, executeQuery()、
2, execute()、
3, executeUpdate()、
4, executeBatch()。
首先创建一个表格book:
create table book( id int auto_increment primary key, name varchar(30), price numeric(5,2), birth datetime ); insert into book values(1,'Oracle',88.88,'2015-07-12 20:42:12'); insert into book values(2,'MySQL',38.66,'2015-07-12 19:22:12');
executeQuery()
@Test public void results() throws SQLException{ Statement st = ConnFactory.getConnection().createStatement(); String sql = "select * from book"; ResultSet res = st.executeQuery(sql); while(res.next()){ Integer id = res.getInt(1); String name = res.getString(2); double price = res.getDouble("price"); //注意!这个是获取日期时间型数据的方式 String birth = res.getDate(4)+" "+res.getTime(4); System.out.println(id+","+name+","+price+","+birth); } ConnFactory.getConnection().close(); }
只能执行查询语句
网络异常,图片无法展示
|
execute()
@Test public void execute() throws SQLException{ Statement st = ConnFactory.getConnection().createStatement(); //测试语句: //测试增加数据 //String sql = "insert into book(name,price,birth) values ('XML',23.30,'2014-09-08 12:00:05' )"; //测试删除数据 //String sql = "delete from book where id='2'"; //测试查询数据 String sql = "select * from book"; //如果第一个结果为一个结果集,则为 true。否则为 false。 boolean boo = st.execute(sql); System.out.println(boo); if(boo){ ResultSet rs = st.getResultSet(); while(rs.next()){ //这里就只输出2个示意一下了。 System.out.println(rs.getInt(1)+","+rs.getString(2)); } } ConnFactory.getConnection().close(); }
增、删、改、查的语句都能够执行。只是查询时返回的结果是告诉成功与否,如果要获取查询结果,得另外用” st.getResultSet()”获取
网络异常,图片无法展示
|
executeUpdate()
@Test public void executeUpdate() throws SQLException{ Statement st = ConnFactory.getConnection().createStatement(); //增加 String sql = "insert into book(name,price,birth) values('红楼梦',85.66,'2013-10-08 12:00:05' )"; //修改 //String sql="update book set price=price*1.1"; //删除 //String sql ="delete from book where id=1"; //查询--不能进行查询!!! //String sql = "select * from book"; int num = st.executeUpdate(sql);//返回值是影响的行数 System.out.println(num); }
只能增、删、改, 不能执行查询语句
executeBatch()
@Test public void batchDemo() throws SQLException{ Connection con = ConnFactory.getConnection(); String sql = "insert into book(name,price ,birth) values ('aaa',11.11,'2013-11-28 19:00:15' )"; Statement st = con.createStatement(); for(int i=0;i<5;i++){ if(i==2){ //错误的MySQL代码! sql = "insert into book(name,price,birth) values('aaa','aa','2013-11-28 19:00:15' )"; } st.addBatch(sql); } sql = "update book set price = price* where price<30"; st.addBatch(sql); int a[] = st.executeBatch(); for(int x:a){ System.out.println(x); } ConnFactory.getConnection().close(); }
//执行批处理—自己本身不带事务,如果其中某条sql语句挂了,则后续的sql执行失败,前面的还是有效的。如果要事务,另外再采用:con.setAutoCommit(false)+try-cacth+ rollback/commit