>데이터 베이스 >MySQL 튜토리얼 >JDBC连接常用数据库 基本的CURD

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

WBOY
WBOY원래의
2016-06-07 15:05:431034검색

欢迎进入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] 

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.