kingbase和mysql_Kingbase人大金仓数据库总结(SQL和JDBC)
人大金仓作为一款国产数据库,使用的人数和相关资料都比较少。
1、SQL语句
创建表:
CREATE TABLE "PUBLIC"."TB_SYS_CONFIGURE"(
"ATTR_KEY" VARCHAR (100) NOT NULL ,
"ATTR_VALUE" VARCHAR (100) NOT NULL
);
添加数据:
INSERT INTO TB_SYS_CONFIGURE
(ATTR_KEY, ATTR_VALUE)
VALUES ('accessCount', '3244'),
('accessCountOne', '3456'),
('accessCountTwo', '7890');
2、JDBC
然后,有了数据以后,测试一下他的JDBC。
引入jar包
下面是我的IDEA里引入jar包的方法,通过Libraries添加
建立连接类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Getcon {undefined
public static final String url = "jdbc:kingbase://192.168.0.211:54322/TEST"; //改为自己数据库地址和名字
public static final String name = "com.kingbase.Driver";
public static final String user = "SYSTEM";
public static final String password = "krms"; //这里的用户名和密码设置为自己的
public Connection conn = null;
public PreparedStatement pst = null;
public Getcon(String sql) {undefined
try {undefined
//Class.forName(name);//指南中的这个方法运行不成功
DriverManager.registerDriver(new com.kingbase.Driver());
conn = DriverManager.getConnection(url, user, password);//获取连接
pst = conn.prepareStatement(sql);//准备执行语句
System.out.print("yes");
} catch (Exception e) {undefined
e.printStackTrace();
}
}
public void close() {undefined
try {undefined
this.conn.close();
this.pst.close();
} catch (SQLException e) {undefined
e.printStackTrace();
}
}
}
操作类-JDBC增删改查
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class KingBaseTest {undefined
static String sql = null;
static Getcon db1 = null;
static ResultSet ret = null;
public static void main(String[] args) {undefined
query();
//update();
//add();
//delete();
}
public static void query(){undefined
sql = "select * from TB_SYS_CONFIGURE;";//要执行的SQL语句,改成自己的表什么的
db1 = new Getcon(sql);//创建数据库对象
try {undefined
ret = db1.pst.executeQuery();//执行语句,ret是结果
while (ret.next()) {undefined
System.out.println(ret.getString(1)+": "+ret.getString(2) );
}//显示数据
ret.close();
db1.close();//关闭连接
} catch (SQLException e) {undefined
e.printStackTrace();
}
}
public static void update(){undefined
sql = "\n" +
"UPDATE TB_SYS_CONFIGURE \n" +
"SET ATTR_KEY='accessCountTest' \n" +
"WHERE ATTR_VALUE='3456';\n";//要执行的SQL语句,改成自己的表什么的
db1 = new Getcon(sql);//创建数据库对象
try {undefined
int i = db1.pst.executeUpdate();
if (i>0){undefined
System.out.println("修改成功");
}
else {undefined
System.out.println("修改失败");
}
ret.close();
db1.close();//关闭连接
} catch (SQLException e) {undefined
e.printStackTrace();
}
}
public static void add(){undefined
sql = "insert into TB_SYS_CONFIGURE (ATTR_KEY,ATTR_VALUE) values(?,?);\n";//要执行的SQL语句,改成自己的表什么的
db1 = new Getcon(sql);//创建数据库对象
PreparedStatement preparedStatement = null;
try {undefined
preparedStatement = db1.conn.prepareStatement(sql);
preparedStatement.setString(1,"accessADD");
preparedStatement.setString(2,"12345");
int i1 = preparedStatement.executeUpdate();
if (i1>0){undefined
System.out.println("修改成功");
}
else {undefined
System.out.println("修改失败");
}
ret.close();
db1.close();//关闭连接
} catch (SQLException e) {undefined
e.printStackTrace();
}
}
public static void delete(){undefined
sql = " delete from TB_SYS_CONFIGURE where ATTR_VALUE=7890 ";
db1 = new Getcon(sql);//创建数据库对象
try {undefined
int i1 = db1.pst.executeUpdate();
if (i1>0){undefined
System.out.println(i1+"个删除成功");
}
else {undefined
System.out.println("删除失败");
}
ret.close();
db1.close();//关闭连接
} catch (SQLException e) {undefined
e.printStackTrace();
}
}
}
————————————————
版权声明:本文为CSDN博主「hikhannah」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_34792357/article/details/113645411
相关文章