My Study Note of JDBC

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了My Study Note of JDBC相关的知识,希望对你有一定的参考价值。

# JDBC -- The Java™ Tutorials
# Study Note of JDBC
# victor
# 2016.05.31

JDBC Study Note ----connect to database

通常,使用JDBC执行SQL语句需要下面5 个步骤:
1> 建立一个连接         | establish a connection
2> 构造一条语句         | create a statement
3> 执行语句             | execute the query
4> 处理结果             | process the resultset object
5> 关闭连接             | close the connection

1. 建立连接
  1.1 Registe Driver
    Class.forName("oracle.jdbc.driver.OracleDriver");
    # 如果使用的是JDBC 4.0 后的版本,这一步可以忽略
  1.2 Get Connection
    con = DriverManger.getconnection(dburl,username,password);
    # dburl 是指JDBC driver 用于连接的数据库
    # dburl中包含了数据库的名字和配置
    # 具体的url语法是由使用的数据库决定的
    
    1.2.1 oracle 数据库的url格式
    jdbc:oracle:<drivertype>:@<database>
    # 具体内容:
    # https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Code:

技术分享
package myjdbc;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class myjdbcutil {
      public String dbms;
      public String dbName; 
      public String userName;
      public String password;
      
      private String serverName;
      private int portNumber;
      private Properties prop;
      
      public myjdbcutil(String filename) throws FileNotFoundException, InvalidPropertiesFormatException, IOException{
          this.setProperties(filename);
      }
      
      /* get properties from .properties file
       * and set these values to Corresponding fields 
       * */
      private void setProperties(String fileName) throws FileNotFoundException,IOException,InvalidPropertiesFormatException {
            this.prop = new Properties();
            InputStream fis =  this.getClass().getResourceAsStream(fileName);
            prop.load(fis);
            
            this.dbms = this.prop.getProperty("dbms");
            this.dbName = this.prop.getProperty("database_name");
            this.userName = this.prop.getProperty("user_name");
            this.password = this.prop.getProperty("password");
            this.serverName = this.prop.getProperty("server_name");
            this.portNumber = Integer.parseInt(this.prop.getProperty("port_number"));
            
            System.out.println("Set the following properties:");
            System.out.println("dbms: " + dbms);
            System.out.println("dbName: " + dbName);
            System.out.println("userName: " + userName);
            System.out.println("password: " + password);
            System.out.println("serverName: " + serverName);
            System.out.println("portNumber: " + portNumber);

      }
      
      /* step 1 to execute query using JDBC
       * get connection with connection properties
       * */
      public Connection getConnection() throws ClassNotFoundException, SQLException{
          Class.forName("oracle.jdbc.driver.OracleDriver");
          System.out.println("Oracle JDBC Driver Registered!");
          
          Connection conn = null;
          Properties connectionProps = new Properties();
          connectionProps.put("user", this.userName);
          connectionProps.put("password", this.password);
          if (this.dbms.equals("oracle")) {
              String dburl = "jdbc:"+this.dbms+":"+dbName+":@"+this.serverName+":"+this.portNumber+"/xe";
              System.out.println("dburl is:"+dburl);
              conn = DriverManager.getConnection(dburl, connectionProps);
              System.out.println("Connected to database successfully!");
          }
          
          return conn;
    }
}
JDBC Connection

Test Code:

技术分享
package myjdbc;


public class TestJdbc {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            myjdbcutil db = new myjdbcutil("mydb.properties");
            db.getConnection();
        }catch(Exception e){
            e.printStackTrace();
        }

    }
}
Test

OutPut:

技术分享
Set the following properties:
dbms: oracle
dbName: thin
userName: test
password: test
serverName: localhost
portNumber: 1522
Oracle JDBC Driver Registered!
dburl is:jdbc:oracle:thin:@localhost:1522/xe
Connected to database successfully!
View Code

 pros:

dbms=oracle
database_name=thin
user_name=test
password=test
server_name=localhost
port_number=1522
jdbc:::@localhost:1522/xe

以上是关于My Study Note of JDBC的主要内容,如果未能解决你的问题,请参考以下文章

Beginning Scala study note Basics of Scala

Shell study note

Beginning Scala study note Trait

study note3

study note10

Beginning Scala study note Pattern Matching