To connect Oracle using MYECLIPSE encoding in a DRP project, first copy the driver Ojdbc14.jar to the corresponding directory, and then write the connection code in the corresponding package.
Package Com.bjpowernode.drp.util;import java.sql.*;p ublic class Test { public static void Main (string[] args) { System.out.println ( Test.getconnection ()); } public static Connection Getconnection () { connection cn = null; try { //Initialize the driver to open the channel connected to the database. class.forname ("Oracle.jdbc.driver.OracleDriver"); //connection string String url = "JDBC:ORACLE:THIN:@192.168.26.206:1521:ORCL"; //database user name and password string user = "SHIQIDRP"; string password = "SHIQIDRP"; cn = Drivermanager.getconnection (URL, user, password); } catch ( ClassNotFoundException e) { e.printstacktrace (); } catch (SQLException e) { e.printstacktrace (); } return cn; }}
problem: in connection string ip:1521, the name of the database is originally filled in, the structure is reported SqlException error, and finally modified in order to ORCL.
Specific error handling you can see: Blog "DRP problem series--the Network Adapter could not establish the connection"
For the strings in the database connection we usually write in the XML configuration file, which is read out when the program is run, so it is easy to maintain and modify.
After modification:
XML: Saving database connection strings, etc.
<?xml version= "1.0" encoding= "UTF-8"?><config> <db-info> <driver-name> Oracle.jdbc.driver.oracledriver</driver-name> <URL>JDBC:ORACLE:THIN:@192.168.26.206:1521:ORCL </url> <user-name>shiqidrp</user-name> <password>shiqidrp</password> </db-info></config>
Jdbcconfig: Defining entities, strings as their properties
Package Com.bjpowernode.drp.util;public class Jdbcconfig {private string drivername;private string Url;private string User;private string Password;public string Getdrivername () {return drivername;} public void Setdrivername (String drivername) {this.drivername = drivername;} Omit the back three get and set methods. }
Xmlconfigreader: Reading a string from an XML file
Add the corresponding jar packages Dom4j-1.6.1.jar and Jaxen-1.1-beta-6.jar
Package Com.bjpowernode.drp.util;import Java.io.inputstream;import Org.dom4j.document;import Org.dom4j.documentexception;import Org.dom4j.element;import Org.dom4j.io.saxreader;public class XmlConfigReader {/* * Parsing Sys-config.xml, using a singleton pattern *///the lazy type, when used in newprivate static xmlconfigreader instance = null;private Jdbcconfig Jdbcconfig = n EW jdbcconfig ();p rivate xmlconfigreader () { saxreader reader = new Saxreader (); InputStream in = Thread. CurrentThread (). Getcontextclassloader (). getResourceAsStream ("config"); try {Document doc = Reader.read (in);// Get JDBC-related configuration information Element Drivernameelt = (Element) doc.selectobject ("/config/db-info/driver-name"); Element Urlelt = (Element) doc.selectobject ("/config/db-info/url"); Element Usernameelt = (Element) doc.selectobject ("/config/db-info/user-name"); Element Passwordelt = (Element) doc.selectobject ("/config/db-info/password");//Set JDBC-related configuration Jdbcconfig.setdrivername (Drivernameelt.getstringvalue ()); Jdbcconfig.seturl (Urlelt.getstringvalue ()); jdbcconfig. SetUser (Usernameelt.getstringvalue ()); Jdbcconfig.setpassword (Passwordelt.getstringvalue ());} catch (Documentexception e) {e.printstacktrace ();}} Returns the JDBC-related configuration public jdbcconfig Getjdbcconfig () {return jdbcconfig;} Singleton mode access entry provided externally, synchronized ensure thread safety public static synchronized Xmlconfigreader getinstance () {if (instance = = null) { Instance = new Xmlconfigreader ();} return instance;} The main method public static void main (string[] args) {jdbcconfig jdbcconfig= xmlconfigreader.getinstance (). Getjdbcconfig (); System.out.println (Jdbcconfig); From the ToString () method that wrote Jdbcconfig}}
Run the display results as follows to successfully read the string in the configuration file
com.bjpowernode.drp.util.jdbcconfig{drivername:oracle.jdbc.driver.oracledriver,url:jdbc:oracle:thin:@ 192.168.26.206:1521:ORCL,USER:SHIQIDRP}
Dbutil: Establishing a connection to the database for related operations
Package Com.bjpowernode.drp.util;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.sqlexception;/* * Encapsulates data common operations. */public class Dbutil {/* * get Connection * /public static Connection getconnection () { Connection conn = Null;try {Jdbcconfig jdbcconfig = Xmlconfigreader.getinstance (). Getjdbcconfig (); Class.forName (Jdbcconfig.getdrivername ()); conn = Drivermanager.getconnection (Jdbcconfig.geturl (), Jdbcconfig.getuser (), Jdbcconfig.getpassword ());} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ();} Return conn;} public static void Main (string[] args) {System.out.println (dbutil.getconnection ()); Test, can be used for the increase and deletion check operation. }}
Other:
JDBC connects to SQL Server and MySQL Database
Connecting to a SQL Server database
Import java.sql.*;p Ublic class Main {public static void main (string [] args) { string drivername= "Com.microsoft.sqlse Rver.jdbc.SQLServerDriver "; String dburl= "Jdbc:sqlserver://localhost:1433;databasename=database";//database Replace with your database name string Username= " Username ";//username replace with your SQL Server login String userpwd=" password ";//password replace with your SQL Server login password try { Class.forName (drivername); Connection dbconn=drivermanager.getconnection (dburl,username,userpwd); SYSTEM.OUT.PRINTLN ("Connection database succeeded"); } catch (Exception e) { System.out.print ("Connection Failed"); } }}
Connect to MySQL Database
Package Test_mysql;import Java.sql.connection;import Java.sql.drivermanager;public class Testmysql {public static void Main (string[] args) { String drivername= "Com.mysql.jdbc.Driver"; String dburl= "Jdbc:mysql://localhost:3306/demo"; Connection URL is jdbc:mysql//server address/database name , the following 2 parameters are login username and password String username= "root"; String userpwd= "MySQL"; try{ Class.forName (drivername); SYSTEM.OUT.PRINTLN ("Load driver succeeded"); } catch (Exception e) { System.out.println ("Load driver failed"); } try { Connection dbconn=drivermanager.getconnection (dburl,username,userpwd); SYSTEM.OUT.PRINTLN ("Connection database succeeded"); } catch (Exception e) { System.out.print ("Connection Failed");}}}
Copyright notice: The shortcomings of this article is unavoidable, please criticize correct, leave valuable comments.
JDBC------Dom4j+xml Connect Oracle