连接数据库,创建表,插入数据,更新数据
连接数据库,创建表,插入数据,更新数据Demo
package com.huawei.demo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; /** * DBtest.java * 演示基于JDBC开发的主要步骤,会涉及创建数据库、创建表、插入数据等 */ class ExitHandler extends Thread { private Statement cancel_stmt = null; public ExitHandler(Statement stmt) { super("Exit Handler"); this.cancel_stmt = stmt; } public void run() { System.out.println("exit handle"); try { if (this.cancel_stmt != null&&!this.cancel_stmt.isClosed()){ this.cancel_stmt.cancel(); } } catch (SQLException e) { System.out.println("cancel query failed."); e.printStackTrace(); } } } public class OLTPConnectDemo { //创建数据库连接。 public static Connection GetConnection(String username, String passwd) { String driver = "com.huawei.gauss.jdbc.ZenithDriver"; String sourceURL = "jdbc:zenith:@192.168.0.1:1888"; Connection conn = null; try { //加载数据库驱动。 Class.forName(driver).newInstance(); } catch (Exception e) { System.out.println("query failed!!!"); e.printStackTrace(); return null; } /** * 创建数据库连接 * getConnection(String url, String user, String password) */ try { conn = DriverManager.getConnection(sourceURL,username,passwd); System.out.println("Connection succeed!"); } catch (Exception e) { e.printStackTrace(); return null; } return conn; }; // 执行普通SQL语句,创建jdbc_test1表。 public static void CreateTable(Connection conn) { Statement stmt = null; try { stmt = conn.createStatement(); // add ctrl+c handler Runtime.getRuntime().addShutdownHook(new ExitHandler(stmt)); // 执行普通SQL语句。 int rc = stmt.executeUpdate("CREATE TABLE IF NOT EXISTS jdbc_test1(col1 INTEGER,col2 VARCHAR(10))"); System.out.println("CREATE TABLE succeed!"); stmt.close(); } catch (SQLException e) { if (stmt != null) { try { stmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } // 执行预处理语句,批量插入数据。 public static void BatchInsertData(Connection conn) { PreparedStatement pst = null; try { // 生成预处理语句。 pst = conn.prepareStatement("INSERT INTO jdbc_test1 VALUES (?,?)"); for (int i = 0; i < 3; i++) { // 添加参数。 pst.setInt(1, i); pst.setString(2, "data " + i); pst.addBatch(); } //执行批处理。 pst.executeBatch(); System.out.println("INSERT INTO succeed!"); pst.close(); } catch (SQLException e) { if (pst != null) { try { pst.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } //执行预编译语句,更新数据。 public static void ExecPreparedSQL(Connection conn) { PreparedStatement pstmt = null; try { pstmt = conn .prepareStatement("UPDATE jdbc_test1 SET col2 = ? WHERE col1 = 1"); pstmt.setString(1, "new Data"); int rowcount = pstmt.executeUpdate(); System.out.println("UPDATE succeed!"); pstmt.close(); } catch (SQLException e) { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } /** * 主程序,逐步调用各静态方法。 * @param args */ public static void main(String[] args) { String userName = "omm"; String password = "gaussdb_123"; // 创建数据库连接。 Connection conn = GetConnection(userName, password); // 创建表。 CreateTable(conn); // 批插数据。 BatchInsertData(conn); // 执行预编译语句,更新数据。 ExecPreparedSQL(conn); // 关闭数据库连接。 try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
做自己的太阳,成为别人的光!