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

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

  }

posted on 2013-09-21 09:15  挖掘者者者  阅读(228)  评论(0编辑  收藏  举报