- //Microsoft Access (ODBC)
- package com.jdm.Access;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import com.jdm.dmdatabase.DMsource;
- public class Access {
- private Connection con;
- private String driverclass;
- private Statement statement;
- private String url;
- private ResultSet result;
- private String username;
- private String password;
- public Access() {
- init();
- try {
- Class.forName(driverclass);
- con = DriverManager.getConnection(url, username, password);
- if (con != null) {
- statement = con.createStatement();
- int a=statement.executeUpdate("insert into user(username,password) values('mike','tome')");
- result = statement.executeQuery("select * from user");
- while (result.next())
- System.out.println("user: " + result.getString(1)
- + "password: " + result.getString(2));
- System.out.print("/n");
- }
- result.close();
- statement.close();
- con.close();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void init() {
- driverclass = "sun.jdbc.odbc.JdbcOdbcDriver";
- url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E://java//test.mdb";
- username = "";
- password = "";
- }
- public static void main(String args[]) {
- new Access();
- }
- }
- ///
- /Microsoft SqlServer2000(需要装sp3补丁,并开放端口)
- //
- package com.jdm.sqlserver;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class SqlLink {
- private Connection con;
- private Statement stm;
- private ResultSet resultset;
- private String driverclass = null;
- private String url = null;
- private String username = null;
- private String password = null;
- public SqlLink() {
- driverclass = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
- url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=cjgl";
- username = "sa";
- password = "123456";
- init();
- if (con != null) {
- try {
- con.close();
- if (con.isClosed())
- System.out.println("连接成功" + "数据源已释放");
- else
- System.out.println("连接成功" + "数据源未释放");
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- } else {
- System.out.println("连接失败");
- }
- }
- public void init() {
- try {
- Class.forName(driverclass);
- con = DriverManager.getConnection(url, username, password);
- stm = con.createStatement();
- /*CallableStatement call=con.prepareCall("{ call ADDXS (?,?,?,?,?,?,?) }");
- call.setString(1,"001118");
- call.setString(2,"王珊珊");
- call.setString(3,"女");
- call.setString(4,"通信工程");
- call.setString(5,"1988-03-21");
- call.setInt(6,40);
- call.setString(7, "良");
- call.execute();*/
- resultset = stm.executeQuery("select * from XS where xb='女'");
- while (resultset.next()) {
- System.out
- .println(resultset.getString(1) + " "
- + resultset.getString(2) + " "
- + resultset.getString(3) + " "
- + resultset.getString(4) + " "
- + resultset.getString(5) + " "
- + resultset.getString(6));
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.out.println("驱动类没有找到");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- new SqlLink();
- }
- }
- ///
- //sun MySql
- package com.jdm.mysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class MySql {
- private String driverclass;
- private String url;
- private String username;
- private String password;
- private Connection con;
- private Statement stm;
- private ResultSet result;
- public MySql() {
- init();
- try {
- Class.forName(driverclass);
- con = DriverManager.getConnection(url, username, password);
- stm = con.createStatement();
- result = stm.executeQuery("SELECT * FROM userinfo");
- while (result.next()) {
- System.out.println("用户名: "+result.getString(1) + "密码: "
- + result.getString(2) + "角色: " + result.getString(3) + "日期: "
- + result.getString(4)+"性别: "+result.getString(4));
- }
- con.close();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void init() {
- driverclass = "com.mysql.jdbc.Driver";
- url = "jdbc:mysql://localhost:3306/test";
- username = "root";
- password = "123456";
- }
- public static void main(String[] args) {
- new MySql();
- }
- }
- ///
- //华工达梦数据库DM
- package com.jdm.dmdatabase;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DMsource {
- private Connection con;
- private String driverclass;
- private Statement statement;
- private String url;
- private ResultSet result;
- private String username;
- private String password;
- public DMsource() {
- init();
- try {
- Class.forName(driverclass);
- con = DriverManager.getConnection(url, username, password);
- if (con != null) {
- System.out.println("连接成功" + con.toString());
- con.close();
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void init() {
- driverclass = "dm.jdbc.driver.DmDriver";
- url = "jdbc:dm://localhost:12345/mdj";
- username = "mdj";
- password = "123456";
- }
- public static void main(String args[]) {
- new DMsource();
- }
- }
- ///
- //ORACLE DATABASE
- //
- package com.jdm.Oracle;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import com.jdm.mysql.MySql;
- public class Oracle {
- private String driverclass;
- private String url;
- private String username;
- private String password;
- private Connection con;
- private Statement stm;
- private ResultSet result;
- public Oracle() {
- init();
- try {
- Class.forName(driverclass);
- con = DriverManager.getConnection(url, username, password);
- stm = con.createStatement();
- result = stm.executeQuery("SELECT * FROM HELP");
- while (result.next()) {
- System.out.println(result.getString(1)+" "+result.getString(2)+" "+result.getString(3));
- }
- if(con!=null)System.out.println("连接成功");
- con.close();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void init() {
- driverclass = "oracle.jdbc.driver.OracleDriver";
- url = "jdbc:oracle:thin:@localhost:1521:XE";
- //url="jdbc:oracle:thin:@computername:host(1521):databasename"
- username = "SYSTEM";
- password = "123456";
- }
- public static void main(String[] args) {
- new Oracle();
- }
- }
- //推荐国产数据库系统 华工达梦 人大金仓 神舟oscr
java连接常用数据库
最新推荐文章于 2024-11-02 15:52:57 发布