Gbase JDBC 应用示例

123WT321 2020-12-29 02:42:05
1.使用 JDBC 创建连接
本示例实现了通过 JDBC 建立数据库链接的功能。
package com.gbase.jdbc.simple;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionSimple {
public static void main(String[] args) {
ConnectionSimple connectionSimple = new
ConnectionSimple();
connectionSimple.userDriverManagerGetConnection();
}
/**
* 使用 DriverManager 获取连接.
*/
public void userDriverManagerGetConnection() {
Connection conn = null;
try {
Class.forName("com.gbase.jdbc.Driver");
conn =
DriverManager.getConnection("jdbc:gbase://192.168.5.210:5258/test?us
er=root&password=");
} catch (SQLException ex) {
// 处理错误
System.out.println("SQLException: " +
ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " +
ex.getErrorCode());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (NullPointerException e) {
} catch (Exception e) {
conn = null;
} } } }

2.通过 JDBC 执行 DDL 和 DML 语句
2.1用例 executeDDLAndDMLSQLByStatement 方法实现以下功能:
使用 Statement 执行 DDL 语句创建一个表;
使用 Statement 执行 DML 语句向表中插入一条数据;
使用 Statement 执行 DML 语句修改 2 中插入的数据。
2.2用例 executeDDLAndDMLSQLByPreparedStatement 方法实现以下功能:
使用 PreparedStatement 执行 DDL 语句创建一个表;
使用 PreparedStatement 执行 DML 语句向表中插入一条数据;
使用 PreparedStatement 执行 DML 语句修改 2 中插入的数据。
示例如下:
package com.gbase.jdbc.simple;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class ExecuteUpdateSQLByStatement {
private static final String URL =
"jdbc:gbase://192.168.XXX.XX:5258/test?user=sysdba&password=";
/**
* @param args
*/
public static void main(String[] args) {
ExecuteUpdateSQLByStatement executeUpdateSQLByStatement =
new ExecuteUpdateSQLByStatement();
executeUpdateSQLByStatement.executeDDLAndDMLSQLByStateme
nt();
executeUpdateSQLByStatement.executeDDLAndDMLSQLByPrepare
dStatement();
}
/**
* 在 test 数据库中创建一个名称为
* “user_info”的表,包含三个字段,
* 并向表中插入三条数据。
*/
public void executeDDLAndDMLSQLByStatement () {
Connection conn = null;
Statement stm = null;
try {
Class.forName("com.gbase.jdbc.Driver");
conn = DriverManager.getConnection(URL);
stm = conn.createStatement();
/*
* create table user_info (
user_id int(11) ,
user_Name varchar(50),
user_info varchar(200)
)ENGINE=GsDB DEFAULT CHARSET=utf8
*/
stm.executeUpdate("drop table if exists `user_info`");
stm.executeUpdate("create table `user_info`
( `user_id` int(11) ,`user_Name` varchar(50),`user_info`
varchar(200))ENGINE=GsDB DEFAULT CHARSET=utf8");
stm.executeUpdate("insert into `user_info`
(`user_id`,`user_name`, `user_info`) values (3,'张五','南大通用
-gbase8d')");
stm.executeUpdate("update `user_info` set `user_name`
= '张五修改' where user_id='3' ");
System.out.println("executeDDLAndDMLSQLByStatement ok");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
stm.close();
} catch (NullPointerException e) {
} catch (Exception e) {
stm = null;
}
try {
conn.close();
} catch (NullPointerException e) {
} catch (Exception e) {
conn = null;
} } }
/**
* 在 test 数据库中创建一个名称为
* “user_info”的表,包含三个字段,
* 并向表中插入三条数据。
*/
public void executeDDLAndDMLSQLByPreparedStatement () {
Connection conn = null;
PreparedStatement stm = null;
try {
Class.forName("com.gbase.jdbc.Driver");
conn = DriverManager.getConnection(URL);
stm = conn.prepareStatement("drop table if exists
`user_info`");
/*
* create table user_info (
user_id int(11) ,
user_Name varchar(50),
user_info varchar(200)
)ENGINE=GsDB DEFAULT CHARSET=utf8
*/
stm.addBatch("create table `user_info-2` ( `user_id`
int(11) ,`user_Name` varchar(50),`user_info` varchar(200))ENGINE=GsDB
DEFAULT CHARSET=utf8");
stm.executeBatch();
stm = conn.prepareStatement("insert into `user_info-2`
(`user_id`,`user_name`, `user_info`) values (?,?,?)");
stm.setInt(1, 3);
stm.setString(2, "张五");
stm.setString(3, "南大通用-gbase8d");
stm.executeUpdate();
stm = conn.prepareStatement("update `user_info-2` set
`user_name` = ? where user_id=? ");
stm.setString(1, "张五修改 Prepared");
stm.setInt(2, 3);
stm.executeUpdate();
System.out.println("executeDDLAndDMLSQLByPreparedSta
tement ok");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
stm.close();
} catch (NullPointerException e) {
} catch (Exception e) {
stm = null;
}
try {
conn.close();
} catch (NullPointerException e) {
} catch (Exception e) {
stm = null;
} } } }
...全文
38 回复 打赏 收藏 举报
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复
相关推荐
一个很好的jsp图书管理系统, public class DB { private final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_Blog"; private final String userName = "sa"; private final String password = "sa"; private Connection con = null; private Statement stm=null; /* 通过构造方法加载数据库驱动 */ public DB() { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); } catch (Exception e) { e.printStackTrace(); System.out.println("加载数据库驱动失败!"); } } /* 创建数据库连接 */ public void createCon() { try { con = DriverManager.getConnection(url, userName, password); } catch (Exception e) { e.printStackTrace(); System.out.println("获取数据库连接失败!"); } } /* 获取Statement对象 */ public void getStm() { createCon(); try { stm=con.createStatement(); } catch (SQLException e) { e.printStackTrace(); System.out.println("创建Statement对象失败!"); } } /** * @功能 对数据库的增加、修改和删除的操作 * @参数 sql为要执行的SQL语句 * @返回值 boolean型值 */ public boolean executeUpdate(String sql) { System.out.println(sql); boolean mark=false; try { getStm(); int iCount = stm.executeUpdate(sql); if(iCount>0) mark=true; else mark=false; } catch (Exception e) { e.printStackTrace(); mark=false; } return mark; } /* 查询数据库 */ public ResultSet executeQuery(String sql) { ResultSet rs=null; try { getStm(); try { rs = stm.executeQuery(sql); } catch (Exception e) { e.printStackTrace(); System.out.println("查询数据库失败!"); } } catch (Exception e) { e.printStackTrace(); } ret
import java.sql.*; public class DataBaseConnection { static{ //2005 //String DBDRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";//2000 } public static Connection getConn(){ String DBDRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String DBURL="jdbc:sqlserver://localhost:1433;DatabaseName=cwgl"; Connection conn=null; try { Class.forName(DBDRIVER); conn=DriverManager.getConnection(DBURL,"sa","1"); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void closeConn(Connection conn){ try { conn.close(); conn=null; } catch (SQLException e) { e.printStackTrace(); } } public static Statement getStmt(Connection conn){ Statement stmt=null; try { stmt=conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } return stmt; } public static void closeStmt(Statement stmt){ try { stmt.close(); stmt=null; } catch (SQLException e) { e.printStackTrace(); } } public static PreparedStatement getpStmt(Connection conn,String sql){ PreparedStatement pStmt=null; try { pStmt=conn.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return pStmt; } public static void closePstmt(Statement pstmt){ try { pstmt.close(); pstmt=null; } catch (SQLException e) { e.printStackTrace(); } } public static ResultSet getRs(Statement stmt,String sql){ ResultSet rs=null; try { rs=stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void closeRs(ResultSet rs){ try { rs.close(); rs=null; } catch (SQLException e) { e.printStackTrace(); } } }
经典java数据库封装类,package com.bjsxt.shopping.util; import java.sql.*; public class DB { public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/shopping?user=root&password=root"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static PreparedStatement prepare(Connection conn, String sql) { PreparedStatement pstmt = null; try { if(conn != null) { pstmt = conn.prepareStatement(sql); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) { PreparedStatement pstmt = null; try { if(conn != null) { pstmt = conn.prepareStatement(sql, autoGenereatedKeys); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static Statement getStatement(Connection conn) { Statement stmt = null; try { if(conn != null) { stmt = conn.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } return stmt; } /* public static ResultSet getResultSet(Connection conn, String sql) { Statement stmt = getStatement(conn); ResultSet rs = getResultSet(stmt, sql); close(stmt); return rs; } */ public static ResultSet getResultSet(Statement stmt, String sql) { ResultSet rs = null; try { if(stmt != null) { rs = stmt.executeQuery(sql); } } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void executeUpdate(Statement stmt, String sql) { try { if(stmt != null) { stmt.executeUpdate(sql); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Connection conn) { try { if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement stmt) { try { if(stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet rs) { try { if(rs != null) { rs.close(); rs = null; } } catch (SQLException e) { e.printStackTrace(); } } }
发帖
其他

238

社区成员

其他产品/厂家
社区管理员
  • 其他
加入社区
帖子事件
创建了帖子
2020-12-29 02:42
社区公告
暂无公告