天天看点

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

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

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

   private string classname="oracle.jdbc.driver.oracledriver";

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

  jdbc连接数据库案例

package com.itlwc;

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

  }

 }

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

    state.close();

   state = null;

  if (conn != null) {

    conn.close();

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

使用替换变量增加

public void add2(student student) {

 string sql = "insert into student values(?,?,?,?,?)";

  ps.setstring(1, student.getcode());

  ps.setstring(2, student.getname());

  ps.setstring(3, student.getsex());

  ps.setstring(4, student.getage());

  ps.setstring(5, student.getid());

  删除方法

使用拼sql删除

public void delete1(string id) {

 string sql = "delete from student where id='" + id+"'";

使用替换变量删除

public void delete2(string id) {

 string sql = "delete from student where id=?";

  ps.setstring(1, id);

 修改方法

使用拼sql修改

public void update1(student student) {

 string sql = "update student set code='" + student.getcode()

   + "',name='" + student.getname() + "',sex=" + student.getsex()

   + ",age=" + student.getage() + " where id=" + student.getid();

使用替换变量修改

public void update2(student student) {

 string sql = "update student set code=?,name=?,sex=?,age=? where id=?";

  查询方法

查询得到一个对象

public student findbyid(int id) {

 string sql = "select * from student where id=" + id;

 student student = new student();

 resultset rs = null;

  rs = ps.executequery();

  if (rs.next()) {

   student.setid(rs.getstring(1));

   student.setcode(rs.getstring(2));

   student.setname(rs.getstring(3));

   student.setsex(rs.getstring(4));

   student.setage(rs.getstring(5));

 dbconnection.close(rs, ps, conn);

 return student;

查询得到一组数据

@suppresswarnings("unchecked")

public list find() {

 string sql = "select * from student";

 list list = new arraylist();

  while (rs.next()) {

   student student = new student();

   list.add(student);

 return list;

 统计数据库总条数

public int getrows() {

 int totalrows = 0;

 string sql = "select count(*) as totalrows from student";

   totalrows = integer.valueof(rs.getstring("totalrows"));

 return totalrows;

  执行存储过程

第一种

 public string retrieveid(string tablename,string interval) throws sqlexception {  

     connection conn = dbconnection.getconn();  

     string sql = "exec p_xt_idbuilder '" + tablename + "','" + interval+ "'";  

     preparedstatement ps = conn.preparestatement(sql);

  resultset rs = ps.executequery();

     string maxid = "";  

     if(rs.next()){  

         maxid = rs.getstring("bh");  

     }  

     dbconnection.close(rs, ps, conn);  

     return maxid;  

第二种

 public string retrieveid(string tablename,string interval) throws sqlexception {

  connection conn = dbconnection.getconn();

  callablestatement cs = conn.preparecall("{call p_xt_idbuilder(?,?,?)}");

     cs.setstring(1, tablename);

     cs.setstring(2, interval);

     cs.registeroutparameter(3,java.sql.types.varchar);

     cs.executeupdate();

     string maxid = "";

     maxid=cs.getstring(3);   

  dbconnection.close(null, cs, conn);

  return maxid;

最新内容请见作者的github页:http://qaseven.github.io/