jdbc 連線各種資料庫
連線Oracle資料庫:
將一下兩個檔案放在src資料夾中,jsp頁面新增如下三行程式碼
<%@ page contentType="text/html; charset=UTF-8"%>//中文字元處理
<%@ page import="java.sql.*"%>
”<jsp:useBean id="dbBean" class="fuji.OptionDB" />
即可使用ResultSet Re = dbBean.getInfo("sql")呼叫sql語句。
檔案OptionDB.java:
package fuji;
import java.sql.*;
public class OptionDB {
private Connection conn;
private Statement stmt;
public OptionDB() {
String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String user = "hanbai";
String pwd = "hanbai";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, pwd);
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 查詢
public ResultSet getInfo(String sql) {
ResultSet rs = null;
try {
System.out.println(sql);
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
// 增加、更改、刪除
public void update(String sql) {
try {
System.out.println(sql);
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 輸出字串格式化
public String formatStr(String inStr) {
String leftStr = inStr.replaceAll("<", "<");
String rightStr = leftStr.replaceAll(">", ">");
String outStr = rightStr.replaceAll(" "," ");
return outStr;
}
// 輸入字串格式化
public String insertStr(String inStr) {
// char[] c = inStr.toCharArray();
// for(int i = 0; i < c.length;i ++){
// int chr = c[i];
// System.out.print(chr + ",");
// }
String outStr = inStr.replaceAll(((char)160)+"","" );
return outStr;
}
// 關閉
public void closeAll() {
try {
stmt.close();
System.out.println("----關閉資料庫連結----");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
檔案UserQueRen.java:
package fuji;
public class UserQueRen {
private String UserStr;
// private String ResultNotNull="不能為空";
// private String ResultPwdE="密碼最多為8位數字或英文字母";
// private String ResultNameE="最多為十位字元";
public void SetStr(String str)
{
this.UserStr=str;
}
public boolean NamequeRen()
{
String temp=UserStr.trim();
if(temp=="")
{
return false;
}
else
{
return true;
}
}
public int PwdQueRen()
{
int x=0;
int flag=1;
if(UserStr.trim()=="")
{
flag=-1;
}
else if(UserStr.trim().length()<UserStr.length())
{
flag=-2;
}
else
{
char[] ch=UserStr.toCharArray();
while(x<UserStr.length())
{
if(!(ch[x]>='0'&&ch[x]<='9'||ch[x]>='a'&&ch[x]<='z'||ch[x]>='A'&&ch[x]<='Z'))
{
flag=0;
}
x++;
}
}
return flag;
}
}
其他資料庫連線替換語句
JDBC 連線 各種資料庫方法
1、Oracle8/8i/9i資料庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl為資料庫的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
2、DB2資料庫
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample為你的資料庫名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
3、Sql Server7.0/2000資料庫
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb為資料庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
4、Sybase資料庫
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";
//myDB為你的資料庫名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
5、Informix資料庫
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//myDB為資料庫名
Connection conn= DriverManager.getConnection(url);
6、MySQL資料庫
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicod
e=true&characterEncoding=8859_1"
//myDB為資料庫名
Connection conn= DriverManager.getConnection(url);
7、PostgreSQL資料庫
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB"
//myDB為資料庫名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);