【DM】教你用 JDBC 连接达梦数据库并进行增删改查,java 项目百度网盘
System.out.print("建表成功");
state.close();
testcon.close();
}
}
===================================================================
public void insert() throws SQLException{
String sql="INSERT INTO TEST VALUES(1,'LALALA')";
Connection testcon=getConn();
PreparedStatement pre=testcon.prepareStatement(sql);
try{
pre.executeUpdate();
}catch(SQLException e){
System.out.print(e);
}
System.out.print("插入成功");
pre.close();
testcon.close();
}
===================================================================
public void update() throws SQLException{
String sql="UPDATE TEST SET C2='HAHAHA' WHERE C1 =1";
Connection testcon=getConn();
PreparedStatement pre=testcon.prepareStatement(sql);
try{
pre.executeUpdate();
}catch(SQLException e){
System.out.print(e);
}
System.out.print("修改成功");
pre.close();
testcon.close();
}
===================================================================
public void select() throws SQLException{
String sql="SELECT * FROM TEST;";
Connection testcon=getConn();
Statement state=testcon.createStatement();
ResultSet res=state.executeQuery(sql);
while(res.next()){
System.out.println(
res.getInt(1)+","+
res.getString(2)
);
}
}
===================================================================
public void delete() throws SQLException{
String sql="DELETE FROM TEST WHERE C1=1";
Connection testcon=getConn();
PreparedStatement pre=testcon.prepareStatement(sql);
try{
pre.executeUpdate();
}catch(SQLException e){
System.out.print(e);
}
System.out.print("删除成功");
pre.close();
testcon.close();
}
===================================================================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.*;
import java.sql.*;
import java.util.Date;
import javafx.scene.control.Tab;
class Connec{
public Connection getConn(){
Connection conn=null;
String name="dm.jdbc.driver.DmDriver";
String url="jdbc:dm://127.0.0.1:5237";
String user="SYSDBA";
String password="wf1105tm0306";
try{
Class.forName(name);
conn=DriverManager.getConnection(url,user,password);
System.out.println("数据库连接成功");
}catch(Exception
e){
System.out.println(e);
}
return conn;
}
}
class Table extends Connec{
public void create() throws SQLException{
String sql_1="DROP TABLE TEST;";
String sql_2="CREATE TABLE TEST(C1 INT,C2 VARCHAR(50));";
Connection testcon=getConn();
Statement state=testcon.createStatement();
try{
state.executeUpdate(sql_2);
}catch(SQLException e){
state.executeUpdate(sql_1);
state.executeUpdate(sql_2);
}
System.out.print("建表成功");
state.close();
testcon.close();
}
public void insert() throws SQLException{
String sql="INSERT INTO TEST VALUES(1,'LALALA')";
Connection testcon=getConn();
PreparedStatement pre=testcon.prepareStatement(sql);
try{
pre.executeUpdate();
}catch(SQLException e){
System.out.print(e);
}
System.out.print("插入成功");
pre.close();
testcon.close();
}
public void update() throws SQLException{
String sql="UPDATE TEST SET C2='HAHAHA' WHERE C1 =1";
Connection testcon=getConn();
PreparedStatement pre=testcon.prepareStatement(sql);
try{
pre.executeUpdate();
}catch(SQLException e){
System.out.print(e);
}
System.out.print("修改成功");
pre.close();
testcon.close();
}
public void delete() throws SQLException{
String sql="DELETE FROM TEST WHERE C1=1";
Connection testcon=getConn();
PreparedStatement pre=testcon.prepareStatement(sql);
try{
pre.executeUpdate();
}catch(SQLException e){
System.out.print(e);
}
System.out.print("删除成功");
pre.close();
testcon.close();
}
评论