java jdbc
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java jdbc,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7718字,纯文字阅读大概需要12分钟。
内容图文
mysql表:
DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `birthday` date DEFAULT NULL, `begintime` time DEFAULT NULL, `begindatetime` datetime DEFAULT NULL, `issupper` bit(1) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
DataSourceManager
package cn.byref.demo.jdbc; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceManager { public static final String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"; publicstaticfinal String USERNAME = "root"; publicstaticfinal String PASSWORD = "xxx"; publicstatic Connection getConnection() throws PropertyVetoException, SQLException{ ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setJdbcUrl(URL); ds.setUser(USERNAME); ds.setPassword(PASSWORD); ds.setDriverClass("com.mysql.jdbc.Driver"); Connection conn = ds.getConnection(); return conn; } }
插入数据测试
package cn.byref.demo.jdbc; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; import com.mchange.v2.c3p0.ComboPooledDataSource; public class InsertTest { public static void main(String[] args) { try { insert(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (PropertyVetoException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static final String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"; public static final String USERNAME = "root"; public static final String PASSWORD = "xxx"; public static void insert() throws SQLException, PropertyVetoException { ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setJdbcUrl(URL); ds.setUser(USERNAME); ds.setPassword(PASSWORD); ds.setDriverClass("com.mysql.jdbc.Driver"); Connection conn = ds.getConnection(); String sql = "insert into test (username,age,birthday,begintime,begindatetime,issupper) " + "values(?,?,?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "王五"); pstmt.setInt(2, 20); System.out.println(Calendar.getInstance().getTime()); Date d = new Date(Calendar.getInstance().getTime().getTime()); System.out.println(d); pstmt.setDate(3, d); Time t = new Time(d.getTime()); pstmt.setTime(4,t); Timestamp ts = new Timestamp(d.getTime()); pstmt.setTimestamp(5, ts); byte b = 1; pstmt.setByte(6, b); int count = pstmt.executeUpdate(); System.out.println("count=" + count); } }
存储过程测试
package cn.byref.demo.jdbc; import java.beans.PropertyVetoException; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; public class TestProc { public static void main(String[] args) { try { // 1.获取Connection对象 Connection conn = DataSourceManager.getConnection(); //2.创建CallableStatment对象 CallableStatement cstmt = conn.prepareCall("{call getTest(?,?,?)}"); //3.1 设置输入参数 cstmt.setInt(1, 1); cstmt.setInt(2, 2); //3.2 设置输出参数 cstmt.registerOutParameter(3, java.sql.Types.INTEGER); //4 执行存储过程 获取结果集 ResultSet rs = cstmt.executeQuery(); //5.打印输出参数 System.out.println("recordcount:" + cstmt.getInt(3)); //6.循环输出结果集while(rs.next()){ java.util.Date d = new java.util.Date(rs.getTimestamp(6).getTime()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date d2 = new java.util.Date(rs.getTime(5).getTime()); SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss"); System.out.println(rs.getString(1) + " - " + rs.getString(2) + "-" + rs.getInt(3) + " " + rs.getDate(4) + " begintime:" + sdf2.format(d2) + " " + sdf.format(d) + "\t" + rs.getBoolean(7)); } rs.close(); conn.close(); } catch (PropertyVetoException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
TestModel.java
package cn.byref.demo.jdbc; import java.util.Date; public class TestModel { private int id; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } private String username; private Date birthday; private Date begintime; private Date begindatetime; privateboolean issupper; publicint getId() { return id; } public TestModel(){} public TestModel(int id, int age, String username, Date birthday, Date begintime, Date begindatetime, boolean issupper) { super(); this.id = id; this.age = age; this.username = username; this.birthday = birthday; this.begintime = begintime; this.begindatetime = begindatetime; this.issupper = issupper; } publicvoid setId(int id) { this.id = id; } public String getUsername() { return username; } publicvoid setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } publicvoid setBirthday(Date birthday) { this.birthday = birthday; } public Date getBegintime() { return begintime; } publicvoid setBegintime(Date begintime) { this.begintime = begintime; } public Date getBegindatetime() { return begindatetime; } publicvoid setBegindatetime(Date begindatetime) { this.begindatetime = begindatetime; } publicboolean isIssupper() { return issupper; } publicvoid setIssupper(boolean issupper) { this.issupper = issupper; } }
事务测试
package cn.byref.demo.jdbc; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; public class TransactionTest { public static void main(String[] args) { java.util.Date d = Calendar.getInstance().getTime(); TestModel model = new TestModel(0, 10, "张三1", d, d, d, true); TestModel model2 = new TestModel(0, 20, "侠客1", d, d, d, false); TransactionTest tt = null; try { tt = new TransactionTest(DataSourceManager.getConnection()); tt.beginTrans(); // tt.setConn(); System.out.println("model:" + tt.insert(model)); int x = 1 / 0; System.out.println("model2:" + tt.insert(model2)); tt.commit(); } catch(Exception e){ try { tt.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e.printStackTrace(); } e.printStackTrace(); } finally { try { tt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public TransactionTest(Connection conn){ this.conn = conn; } private Connection conn; publicvoid setConn(Connection conn) { this.conn = conn; } publicvoid beginTrans() throws SQLException { if (this.conn != null) conn.setAutoCommit(false); } publicvoid commit() throws SQLException { if (this.conn != null) conn.commit(); } publicvoid rollback() throws SQLException{ if(this.conn != null) this.conn.rollback(); } publicvoid close() throws SQLException { if (this.conn != null) this.conn.close(); } String sql = "insert into test (username,age,birthday,begintime,begindatetime,issupper) values(?,?,?,?,?,?)"; publicint insert(TestModel model) throws SQLException { if (model == null) { thrownew IllegalArgumentException("model is null"); } PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, model.getUsername()); pstmt.setInt(2, model.getAge()); pstmt.setDate(3, new Date(model.getBirthday().getTime())); pstmt.setTime(4, new Time(model.getBegintime().getTime())); pstmt.setTimestamp(5, new Timestamp(model.getBegindatetime().getTime())); pstmt.setBoolean(6, model.isIssupper()); return pstmt.executeUpdate(); } }
原文:http://www.cnblogs.com/byxxw/p/4892380.html
内容总结
以上是互联网集市为您收集整理的java jdbc全部内容,希望文章能够帮你解决java jdbc所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。