java 向DB2插入数据
来源:互联网 发布:web of science数据库 编辑:程序博客网 时间:2024/05/21 06:59
PreparedStatement语句插入数据
标签: jdbcselect对象数据
2016-10-13 22:22 3864人阅读 评论(0)收藏举报
分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
代码:
1.直接调用插入:
DButil.java
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
- import com.mysql.jdbc.Connection;
- public class DButil {
- /*
- * 打开数据库
- */
- private static String driver;//连接数据库的驱动
- private static String url;
- private static String username;
- private static String password;
- static {
- driver="com.mysql.jdbc.Driver";//需要的数据库驱动
- url="jdbc:mysql://localhost:3306/test";//数据库名路径
- username="root";
- password="root";
- }
- public static Connection open()
- {
- try {
- Class.forName(driver);
- return (Connection) DriverManager.getConnection(url,username, password);
- } catch (Exception e) {
- System.out.println("数据库连接失败!");
- // TODO Auto-generated catch block
- e.printStackTrace();
- }//加载驱动
- return null;
- }
- /*
- * 关闭数据库
- */
- public static void close(Connection conn)
- {
- if(conn!=null)
- {
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
jdbcconnection.java
- import java.sql.SQLException;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- public class jdbcconnection {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- insert("yangxu","yangxu@qq.com");
- }
- static void insert(String name,String email)
- {
- String sql="insert into Haige(name,email) value(?,?)";
- Connection conn=DButil.open();
- try {
- PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
- pstmt.setString(1,name);
- pstmt.setString(2,email);
- pstmt.executeUpdate();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally {
- DButil.close(conn);
- }
- }
- }
2.面向对象的方式
Customer.java
- public class Customer {
- int id;
- String name;
- String email;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- }
jdbcconnection.java(插入数据)
- import java.sql.SQLException;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- public class jdbcconnection {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //insert("yangxu","yangxu@qq.com");
- Customer c=new Customer();
- c.setName("zhangbing");
- c.setEmail("zhangbing@qq.com");
- insert(c);
- }
- static void insert(Customer c)
- {
- String sql="insert into Haige(name,email) value(?,?)";
- Connection conn=DButil.open();
- try {
- PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
- pstmt.setString(1,c.getName());
- pstmt.setString(2,c.getEmail());
- pstmt.executeUpdate();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally {
- DButil.close(conn);
- }
- }
- }
jdbcconnection.java(修改数据)
- import java.sql.SQLException;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- public class jdbcconnection {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //insert("yangxu","yangxu@qq.com");
- Customer c=new Customer();
- // c.setName("zhangbing");
- // c.setEmail("zhangbing@qq.com");
- //insert(c);
- c.setId(1001);
- c.setName("kaixin");
- Update(c);
- }
- static void Update(Customer c)
- {
- String sql="update haige set name=? where id=?";
- Connection conn=DButil.open();
- try {
- PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
- pstmt.setString(1,c.getName());
- pstmt.setInt(2,c.getId());;
- pstmt.executeUpdate();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally {
- DButil.close(conn);
- }
- }
- }
jdbcconnection.java(删除数据)
- import java.sql.SQLException;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- public class jdbcconnection {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //insert("yangxu","yangxu@qq.com");
- // Customer c=new Customer();
- //// c.setName("zhangbing");
- //// c.setEmail("zhangbing@qq.com");
- // //insert(c);
- // c.setId(1001);
- // c.setName("kaixin");
- // Update(c);
- delete(1006);
- }
- static void delete(int id)
- {
- String sql="delete from haige where id=?";
- Connection conn=DButil.open();
- try {
- PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
- pstmt.setInt(1,id);;
- pstmt.executeUpdate();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally {
- DButil.close(conn);
- }
- }
- }
jdbcconnection.java(查询数据)
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- public class jdbcconnection {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //insert("yangxu","yangxu@qq.com");
- // Customer c=new Customer();
- //// c.setName("zhangbing");
- //// c.setEmail("zhangbing@qq.com");
- // //insert(c);
- // c.setId(1001);
- // c.setName("kaixin");
- // Update(c);
- //delete(1006);
- Customer c=query(1005);
- System.out.println(c.getId()+","+c.getName()+","+c.getEmail());
- }
- static Customer query(int id)
- {
- String sql="select * from haige where id=?";
- Connection conn=DButil.open();
- try {
- PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
- pstmt.setInt(1,id);
- ResultSet rs=pstmt.executeQuery();
- if(rs.next())
- {
- String name=rs.getString(2);
- String email=rs.getString(3);
- Customer c=new Customer();
- c.setId(id);
- c.setName(name);
- c.setEmail(email);
- return c;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally {
- DButil.close(conn);
- }
- return null;
- }
- }
阅读全文
0 0
- java 向DB2插入数据
- DB2大量数据插入
- java向mysql插入数据乱码问题解决
- java向mysql插入数据乱码问题解决
- java jdbc向数据库插入大量数据
- Java向mysql中插入数据
- 向db2中插入大量数据处理方法
- 向DropDownList插入数据
- db2数据插入出现错误!!
- db2插入数据乱码问题?
- db2数据插入错误SQL0000W
- 用java程序向数据库插入数据遇到的几个问题
- 【基础】关于Java向数据库中插入DateTime类型数据
- 运用Java JDBC编写程序向MySQL中插入数据
- java向数据库中插入数据,数据库乱码问题解决
- java程序向mysql数据库插入数据中文 乱码问题解决
- java程序向mysql中插入数据的中文乱码
- Java向mysql中插入数据优化问题
- loadrunner Lr_类函数之lr_decrypt()
- iterm2自动登陆,解决分栏后vi混乱
- 欧拉角和旋转矩阵相互转换
- 【SSH】Hibernate学习(一)
- 线性回归
- java 向DB2插入数据
- 昂贵的聘礼(dijkstra)
- [LeetCode-Algorithms-10] "Regular Expression Matching" (2017.10.12-WEEK6)
- string转换为LPCWSTR
- 中点bresenham算法画线
- Lesson 3 上机练习题——继承
- [HNOI2004]打鼹鼠
- opencv归一化函数normalize详解
- UVALive