1阔蛉,配置文件jdbcdome.properties
username=abc
password=123
2祖屏,簡單實(shí)現(xiàn)
public class Jdbctest {
//通過配置文件獲取相關(guān)信息
private static String UserName;
private static String Password;
public static void main(String[] args) {
// readpropByProperties();
readpropByConfiguration();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
// jdbclinkdatabysqljdbc();
jdbclinkdatabyjtds();
}
//使用使用流和使用Java.util.Properties讀取配置文件
private static void readpropByProperties() {
try {
FileInputStream fin=new FileInputStream("src/jdbcdome.properties"); // 打開文件
Properties props=new Properties();? ? ? ? ? ? ? ? // 建立屬性類
props.load(fin);// 讀入文件
fin.close(); // 關(guān)閉文件
UserName = props.getProperty("username");
Password = props.getProperty("password");
}catch (Exception e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}? ? ? ? ?
}
//使用Configuration讀取配置文件
private static void readpropByConfiguration() {
Configuration config;
try {
config = new PropertiesConfiguration("src/jdbcdome.properties");
UserName = config.getString("username");?
Password = config.getString("password");
} catch (ConfigurationException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}?
}
//使用SQL官方的數(shù)據(jù)庫驅(qū)動sqljdbc(連接不成功)
private static void jdbclinkdatabysqljdbc() {
// String UserName = "abc";//用戶名?
// ? ? String Password = "123";//密碼?
? ? Connection con = null;
? ? PreparedStatement pst = null;//創(chuàng)建Statement?
? ? ? ? ResultSet rs = null;//ResultSet類似Cursor?
try {
//申明數(shù)據(jù)庫驅(qū)動援奢,數(shù)據(jù)庫類型不同蓖谢,加載的驅(qū)動也不相同
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//連接數(shù)據(jù)庫
//jdbc.url=jdbc:sqlserver://192.168.1.121:1433;DatabaseName=xmxc
con = DriverManager.getConnection(
//這行的訪問格式是錯誤的
// "jdbc:microsoft:sqlserver://192.168.1.121:1433;DatabaseName=xmxc",
//這行的格式才是正確的
"jdbc:sqlserver://192.168.1.121:1433;DatabaseName=xmxc",
UserName, Password );
//創(chuàng)建prepareStatement
pst = con.prepareStatement("select * from B_USER where login_name = ?");
//對占位符設(shè)置值根吁,占位符順序從1開始片林,第一個參數(shù)是占位符的位置端盆,第二個參數(shù)是占位符的值。
pst.setString(1, "18602938123");
//獲取查詢結(jié)果费封,ResultSet類似Cursor
rs=pst.executeQuery();
//遍歷查詢結(jié)果
while(rs.next()){
String Name = rs.getString("full_name");
String ID = rs.getString("china_id");
String Pushid = rs.getString("push_id");
System.out.println(Name+";"+ID+";"+Pushid);
}
con.close();
pst.close();
} catch (Exception e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
} finally{
if (con != null)?
? ? ? ? ? ? ? ? try {?
? ? ? ? ? ? ? ? ? ? con.close();
? ? ? ? ? ? ? ? ? ? con = null;
? ? ? ? ? ? ? ? } catch (Exception e) {?
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }?
}
}
//使用jtds數(shù)據(jù)庫驅(qū)動
private static void jdbclinkdatabyjtds() {
// String UserName = "abc";//用戶名?
// ? ? String Password = "123";//密碼?
? ? Connection con = null;
? ? PreparedStatement pst = null;//創(chuàng)建Statement?
? ? ? ? ResultSet rs = null;//ResultSet類似Cursor?
? ? ? ? try {
//申明數(shù)據(jù)庫驅(qū)動焕妙,數(shù)據(jù)庫類型不同,加載的驅(qū)動也不相同
Class.forName("net.sourceforge.jtds.jdbc.Driver");
//連接數(shù)據(jù)庫
//jdbc.url=jdbc:sqlserver://192.168.1.121:1433;DatabaseName=xmxc
con = DriverManager.getConnection(
// "jdbc:sqlserver://192.168.1.121:1433;DatabaseName=xmxc",
// UserName, Password );
"jdbc:jtds:sqlserver://192.168.1.121:1433/xmxc", UserName,?
Password);?
//創(chuàng)建prepareStatement
pst = con.prepareStatement("select * from B_USER where login_name = ?");
//對占位符設(shè)置值弓摘,占位符順序從1開始焚鹊,第一個參數(shù)是占位符的位置,第二個參數(shù)是占位符的值韧献。
pst.setString(1, "18602938123");
//獲取查詢結(jié)果末患,ResultSet類似Cursor
rs=pst.executeQuery();
//遍歷查詢結(jié)果
while(rs.next()){
String Name = rs.getString("full_name");
String ID = rs.getString("china_id");
String Pushid = rs.getString("push_id");
System.out.println(Name+";"+ID+";"+Pushid);
}
con.close();
pst.close();
} catch (Exception e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
} finally{
if (con != null)?
? ? ? ? ? ? ? ? try {?
? ? ? ? ? ? ? ? ? ? con.close();
? ? ? ? ? ? ? ? ? ? con = null;
? ? ? ? ? ? ? ? } catch (Exception e) {?
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }?
}
}
}