写点什么

【DM】教你用 JDBC 连接达梦数据库并进行增删改查,java 项目百度网盘

作者:Java高工P7
  • 2021 年 11 月 10 日
  • 本文字数:1755 字

    阅读完需:约 6 分钟

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


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


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();


}

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
【DM】教你用JDBC连接达梦数据库并进行增删改查,java项目百度网盘