jdbc

/** 
* @(#) uu.java  1.1  2010-9-21

* Copyright 2009 WorldUnion Properties, Ltd. All rights reserved.
* WorldUnion PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package DBUtil;

import java.sql.*;

public class jdbcthin {
//dbUrl数据库连接串信息,其中“1521”为端口,“ora9”为sid
String dbUrl = "jdbc:oracle:thin:@192.168.12.196:1521:portaltest";
//theUser为数据库用户名 jdbc:oracle:thin:@192.168.12.196:1521:portaltest
String theUser = "appuser";
//thePw为数据库密码
String thePw = "appuser";
//几个数据库变量
Connection c = null;
Statement conn;
ResultSet rs = null;

//初始化连接
public jdbcthin() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//与url指定的数据源建立连接
c = DriverManager.getConnection(dbUrl, theUser, thePw);
//采用Statement进行查询
conn = c.createStatement();
System.out.println("naming: success" );
} catch (Exception e) {
e.printStackTrace();
System.out.println("naming:===" + e.getMessage());
}
}

//执行查询
public ResultSet executeQuery(String userName) {
rs = null;
try {
String sql ="select count(1) from t_sso_user t where t.user_name='"+userName+"'";
rs = conn.executeQuery(sql);

} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}

public void executeUpdate(String userName,String passWd){
String sql =" insert into t_sso_user t(t.id, t.user_name, t.user_password, t.create_date, t.last_update_date)  values(SSO_USER_SEQUENCE.Nextval,'"+userName+"','"+passWd+"',sysdate,sysdate)";

try {
conn.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}

public void close() {
try {
conn.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
ResultSet newrs;
jdbcthin newjdbc = new jdbcthin();
newrs = newjdbc.executeQuery("0020278");

try {
System.err.println(newrs.getInt(1));
} catch (SQLException e) {
e.printStackTrace();
}


/*try {
while (newrs.next()) {
System.out.print(newrs.getInt("id"));
System.out.println(":"+newrs.getString("USER_NAME"));
}
} catch (Exception e) {
e.printStackTrace();
} */

//newjdbc.executeUpdate("0020278","Comingppp");
newjdbc.close();
}
}

你可能感兴趣的:(oracle,sql,c,jdbc,SSO)