JDBC访问数据库的步骤

来源:互联网 发布:淘宝网卧室台灯 编辑:程序博客网 时间:2024/05/22 03:27

JDBC访问数据库的步骤

1、加载对应数据库驱动,一般用反射Class.forName(String driverName); 
2、创建连接通过DriverManager.getConnection(url,userName,passwd); 
3、获取statement对象,通过statement对象执行sql语句;
4、如果是查询则返回结果集,贮存于ResultSet
5、如果关闭事物自动提交要提交事物;
6、最后,关闭所有连接,自里往外关闭。

 

 

1 将数据库的JDBC驱动加载到classpath中,在基于JAVAEEWEB应用实际开发过程中,通常要把目标数据库产品的JDBC驱动复制到WEB-INF/lib.
2
加载JDBC驱动,并将其注册到DriverManager中,下面是一些主流数据库的JDBC驱动加裁注册的代码:
  //Oracle8/8i/9iO
数据库(thin模式)
 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
  //Sql Server7.0/2000
数据库
  Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
  //DB2
数据库
  Class.froName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
  //Informix
数据库
  Class.forName("com.informix.jdbc.IfxDriver").newInstance();
  //Sybase
数据库
  Class.forName("com.sybase.jdbc.SybDriver").newInstance();
  //MySQL
数据库
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  //PostgreSQL
数据库
  Class.forNaem("org.postgresql.Driver").newInstance();
3
建立数据库连接,取得Connection对象.例如:
  //Oracle8/8i/9i
数据库(thin模式)
  String url="jdbc:oracle:thin:@localhost:1521:orcl";
  String user="scott";
  String password="tiger";
  Connection conn=DriverManager.getConnection(url,user,password);
 
  //Sql Server7.0/2000
数据库
  String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
  String user="sa";
  String password="";
  Connection conn=DriverManager.getConnection(url,user,password);
 
  //DB2
数据库
  String url="jdbc:db2://localhost:5000/sample";
  String user="amdin"
  String password=-"";
  Connection conn=DriverManager.getConnection(url,user,password);
 
  //Informix
数据库
  Stringurl="jdbc:informix-sqli://localhost:1533/testDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword";
  Connection conn=DriverManager.getConnection(url);
 
  //Sybase
数据库
  String url="jdbc:sybase:Tds:localhost:5007/tsdata";
  Properties sysProps=System.getProperties();
  SysProps.put("user","userid");
  SysProps.put("password","user_password");
  Connection conn=DriverManager.getConnection(url,SysProps);
 
  //MySQL
数据库
  Stringurl="jdbc:mysql://localhost:3306/testDB?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
  Connection conn=DriverManager.getConnection(url);
 
  //PostgreSQL
数据库
  String url="jdbc:postgresql://localhost/testDB";
  String user="myuser";
  String password="mypassword";
  Connection conn=DriverManager.getConnection(url,user,password);
4
建立Statement对象或PreparedStatement对象.例如:
  //
建立Statement对象
  Statement stmt=conn.createStatement();
  //
建立ProparedStatement对象
  String sql="select * from user where userName=? andpassword=?";
  PreparedStatement pstmt=Conn.prepareStatement(sql);
  pstmt.setString(1,"admin");
  pstmt.setString(2,"liubin");
5
执行SQL语句.例如:
  String sql="select * from users";
  ResultSet rs=stmt.executeQuery(sql);
  //
执行动态SQL查询
  ResultSet rs=pstmt.executeQuery();
  //
执行insertupdate delete等语句,先定义sql
  stmt.executeUpdate(sql);
6
访问结果记录集ResultSet对象。例如:
  while(rs.next)
  {
  out.println("
你的第一个字段内容为:"+rs.getString());
  out.println("
你的第二个字段内容为:"+rs.getString(2));
  }
7
依次将ResultSetStatementPreparedStatementConnection对象关闭,释放所占用的资源.例如:
  rs.close();
  stmt.clost();
  pstmt.close();
  con.close();

>>>>>>>>>>>>>>
后加上

MySQL
   
    StringDriver="com.mysql.jdbc.Driver";    //
驱动程序
    StringURL="jdbc:mysql://localhost:3306/db_name";    //
连接的URL,db_name为数据库名   
    String Username="username";    //
用户名
    String Password="password";    //
密码
    Class.forName(Driver).new Instance();
    Connection con=DriverManager.getConnection(URL,Username,Password);
Microsoft SQL Server 2.0
驱动(3jar的那个):
    StringDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";   //
连接SQL数据库的方法
    StringURL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name";   //db_name
为数据库名
    String Username="username";    //
用户名
    String Password="password";    //
密码
    Class.forName(Driver).new Instance();    //
加载数据可驱动
    Connectioncon=DriverManager.getConnection(URL,UserName,Password);    //
Microsoft SQL Server 3.0
驱动(1jar的那个): //老紫竹完善
    StringDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";   //
连接SQL数据库的方法
    StringURL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name";   //db_name
为数据库名
    String Username="username";    //
用户名
    String Password="password";    //
密码
    Class.forName(Driver).new Instance();    //
加载数据可驱动
    Connectioncon=DriverManager.getConnection(URL,UserName,Password);    //
Sysbase:
    StringDriver="com.sybase.jdbc.SybDriver";    //
驱动程序
    String URL="jdbc:Sysbase://localhost:5007/db_name";   //db_name
为数据可名
    String Username="username";    //
用户名
    String Password="password";    //
密码
    Class.forName(Driver).newInstance();   
    Connectioncon=DriverManager.getConnection(URL,Username,Password);
Oracle(
thin模式):
    StringDriver="oracle.jdbc.driver.OracleDriver";    //
连接数据库的方法
    StringURL="jdbc:oracle:thin:@loaclhost:1521:orcl";    //orcl
为数据库的SID
    String Username="username";    //
用户名
    String Password="password";    //
密码
    Class.forName(Driver).newInstance();    //
加载数据库驱动
    Connectioncon=DriverManager.getConnection(URL,Username,Password);   
PostgreSQL:
    StringDriver="org.postgresql.Driver";    //
连接数据库的方法
    StringURL="jdbc:postgresql://localhost/db_name";   //db_name
为数据可名
    String Username="username";    //
用户名
    String Password="password";    //
密码
    Class.forName(Driver).newInstance();   
    Connectioncon=DriverManager.getConnection(URL,Username,Password);
DB2

    StringDriver="com.ibm.db2.jdbc.app.DB2.Driver";    //
连接具有DB2客户端的Provider实例
    //StringDriver="com.ibm.db2.jdbc.net.DB2.Driver";    //
连接不具有DB2客户端的Provider实例
    StringURL="jdbc:db2://localhost:5000/db_name";    //db_name
为数据可名
    String Username="username";    //
用户名
    String Password="password";    //
密码
    Class.forName(Driver).newInstance();   
    Connectioncon=DriverManager.getConnection(URL,Username,Password);
Informix:
    StringDriver="com.informix.jdbc.IfxDriver";   
    StringURL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver";   //db_name
为数据可名
    String Username="username";    //
用户名
    String Password="password";    //
密码
    Class.forName(Driver).newInstance();   
    Connectioncon=DriverManager.getConnection(URL,Username,Password);
JDBC-ODBC:
    String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
    String URL="jdbc:odbc:dbsource";   //dbsource
为数据源名
    String Username="username";    //
用户名
    String Password="password";    //
密码
    Class.forName(Driver).newInstance();   
    Connection con=DriverManager.getConnection(URL,Username,Password);

原创粉丝点击