您的当前位置:首页正文

JDBC连接常用数据库基本的CURD

2020-11-09 来源:赴品旅游

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入 JDBC连接各个数据库的className与url JDBC连接DB2 private String className="com.ibm.db2.jdbc.net.DB2Driver"; private String url="jdbc:db2://localhost:8080/lwc"; JDBC连接Microsoft SQLServer

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  JDBC连接各个数据库的className与url

  JDBC连接DB2

  private String className="com.ibm.db2.jdbc.net.DB2Driver";

  private String url="jdbc:db2://localhost:8080/lwc";

  JDBC连接Microsoft SQLServer(microsoft)

  private String className="com.microsoft.jdbc.sqlserver.SQLServerDriver";

  private String url="jdbc:microsoft:sqlserver://

  localhost:1433;SelectMethod=Cursor;dataBaseName=lwc";

  JDBC连接Sybase(jconn2.jar)

  private String className="com.sybase.jdbc2.jdbc.SybDriver";

  private String url="jdbc:sybase:Tds:localhost:2638";

  JDBC连接MySQL(mm.mysql-3.0.2-bin.jar)

  private String className="org.gjt.mm.mysql.Driver";

  private String url="jdbc:mysql://localhost:3306/lwc";

  JDBC连接PostgreSQL(pgjdbc2.jar)

  private String className="org.postgresql.Driver";

  private String url="jdbc:postgresql://localhost/lwc";

  JDBC连接Oracle(classes12.jar)

  private String className="oracle.jdbc.driver.OracleDriver";

  private String url="jdbc:oracle:thin:@localhost:1521:lwc";

  JDBC连接数据库案例

  package com.itlwc;

  import java.sql.Connection;

  import java.sql.DriverManager;

  import java.sql.ResultSet;

  import java.sql.SQLException;

  import java.sql.Statement;

  public class DBConnection {

  private static Connection conn = null;

  private String user = "";

  private String password = "";

  private String className = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

  private String url = "jdbc:microsoft:sqlserver://"

  + "localhost:1433;SelectMethod=Cursor;dataBaseName=lwc";

  private DBConnection() {

  try {

  Class.forName(this.className);

  conn = DriverManager.getConnection(url, user, password);

  System.out.println("连接数据库成功");

  } catch (ClassNotFoundException e) {

  System.out.println("连接数据库失败");

  } catch (SQLException e) {

  System.out.println("连接数据库失败");

  }

  }

  public static Connection getConn() {

  if (conn == null) {

  conn = (Connection) new DBConnection();

  }

  return conn;

  }

  // 关闭数据库

  public static void close(ResultSet rs, Statement state, Connection conn) {

  if (rs != null) {

  try {

  rs.close();

  } catch (SQLException e) {

  e.printStackTrace();

  }

  rs = null;

  }

  if (state != null) {

  try {

  state.close();

  } catch (SQLException e) {

  e.printStackTrace();

  }

  state = null;

  }

  if (conn != null) {

  try {

  conn.close();

  } catch (SQLException e) {

  e.printStackTrace();

  }

  conn = null;

  }

  }

  // 测试数据库连接是否成功

  public static void main(String[] args) {

  getConn();

  }

  }

  基本CURD

  获取数据库连接请查考JDBC连接常用数据库

  private Connection conn = DBConnection.getConn();

  增加方法

  使用拼sql增加

  public void add1(Student student) {

  String sql = "insert into student values(" + student.getId() + ",'"

  + student.getCode() + "','" + student.getName() + "',"

  + student.getSex() + "," + student.getAge() + ")";

  PreparedStatement ps = null;

  try {

  ps = conn.prepareStatement(sql);

  ps.executeUpdate();

  } catch (SQLException e) {

  e.printStackTrace();

  }

  DBConnection.close(null, ps, conn);

  }

[1] [2] [3]

显示全文