你的這段代碼是獲取配置文件中數(shù)據(jù)庫(kù)配置信息
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供鹿寨網(wǎng)站建設(shè)、鹿寨做網(wǎng)站、鹿寨網(wǎng)站設(shè)計(jì)、鹿寨網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、鹿寨企業(yè)網(wǎng)站模板建站服務(wù),10余年鹿寨做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
配置內(nèi)容都在DBConfig.txt 這個(gè)文件中了
package dda;
import java.sql.*;
import java.io.LineNumberReader;
import java.io.FileReader;
import java.util.StringTokenizer;
/**
*
* 讀取配置文件DBConfig.txt中的配置信息
* @author xxx
* @version 1.0
*/
public class DbConnection {
Connection conn = null;
ResultSet rs = null;
boolean b;
public DbConnection() {
this.b = false;
this.getDBConfig();
}
/* 定義變量 */
String strSeparate = " ";
String strFileName = "DBConfig.txt";
String strUserID = null;
String strUserPWD = null;
String strDB = null;
public void getDBConfig() {
this.getDBConfig();
try {
LineNumberReader oLineNumberReader = new LineNumberReader(
new FileReader(this.strFileName)); // 從 DBConfig.txt 文件中讀取數(shù)據(jù)庫(kù)配置
String strLine = null;
while ((strLine = oLineNumberReader.readLine()) != null) {// 遍歷配置文件中每一行數(shù)據(jù)
StringTokenizer token = new StringTokenizer(strLine,(new String(this.strSeparate).toString())); // 按空格拆分配置內(nèi)容
/* 以下三行都是給屬性設(shè)值,獲取配置屬性 */
this.strDB = token.nextToken();
this.strUserID = token.nextToken();
this.strUserPWD = token.nextToken();
}
} catch (Exception e) {
System.out.println("DB:"+strFileName);
}
}
}
數(shù)據(jù)庫(kù)內(nèi)容
java源代碼 (代碼實(shí)現(xiàn)的是查詢成績(jī)?yōu)?的人員信息 至于其他功能的 代碼中有注釋) 注意 在eclipse里運(yùn)行程序的時(shí)候 要工程里插入jar包 否則運(yùn)行異常! import java sql *; import java io *; class database_manage { ?public Connection conn = null; ?public ResultSet rs = null; ?private String DatabaseDriver = " mysql jdbc Driver"; ?// DataSource 數(shù)據(jù)源名稱DSN ?private String DatabaseConnStr = "jdbc:mysql://localhost: /people_manage?useUnicode=truecharacterEncoding=utf " ?+ " root root"; ?public void setDatabaseDriver(String Driver) { this DatabaseDriver = Driver; ?} ?public String getDatabaseDriver() { return (this DatabaseDriver); ?} ?public void setDatabaseConnStr(String ConnStr) { this DatabaseConnStr = ConnStr; ?} ?public String getDatabaseConnStr() { return (this DatabaseConnStr); ?} ?public database_manage() {// 構(gòu)造函數(shù)連接數(shù)據(jù)庫(kù) try { ?Class forName(DatabaseDriver); } catch (java lang ClassNotFoundException e) { ?System err println("加載驅(qū)動(dòng)器有錯(cuò)誤:" + e getMessage()); ?System out print("執(zhí)行插入有錯(cuò)誤:" + e getMessage());// 輸出到客戶端 } ?} ?public ResultSet query(String sql) {// 查詢數(shù)據(jù)庫(kù) rs = null; try { ?conn = DriverManager ? getConnection( ?"jdbc:mysql://localhost: /people_manage?useUnicode=truecharacterEncoding=utf " ?"root" "root"); ?Statement stmt = conn createStatement(); ?rs = stmt executeQuery(sql); } catch (SQLException ex) { ?System err println("執(zhí)行查詢有錯(cuò)誤:" + ex getMessage()); ?System out print("執(zhí)行查詢有錯(cuò)誤:" + ex getMessage()); // 輸出到客戶端 }
return rs;
} ?public int update_database(String sql) {// 更新或插入數(shù)據(jù)庫(kù) int num = ; try { ?conn = DriverManager ? getConnection( ?"jdbc:mysql://localhost: /people_manage?useUnicode=truecharacterEncoding=utf " ?"root" "root"); ?Statement stmt = conn createStatement(); ?num = stmt executeUpdate(sql); } catch (SQLException ex) { ?System err println("執(zhí)行插入有錯(cuò)誤:" + ex getMessage()); ?System out print("執(zhí)行插入有錯(cuò)誤:" + ex getMessage());// 輸出到客戶端 } CloseDataBase(); return num; ?} ?public void CloseDataBase() {// 關(guān)閉數(shù)據(jù)庫(kù) try { ?conn close(); } catch (Exception end) { ?System err println("執(zhí)行關(guān)閉Connection對(duì)象有錯(cuò)誤 " + end getMessage()); ?System out print("執(zhí)行執(zhí)行關(guān)閉Connection對(duì)象有錯(cuò)誤 有錯(cuò)誤:" + end getMessage()); // 輸出到客戶端 } ?} } class people { ?private String uid; ?private String name; ?private String banji; ?private int score; ?public people() { ?} ?public people(String uid String name String banji) { this uid = uid; this name = name; this banji = banji; ?} ?public people(String uid String name String banji int score) { this uid = uid; this name = name; this banji = banji; this score = score; ?} ?public String getUid() { return uid; ?} ?public void setUid(String uid) { this uid = uid; ?} ?public String getName() { return name; ?} ?public void setName(String name) { this name = name; ?} ?public String getBanji() { return banji; ?} ?public void setBanji(String banji) { this banji = banji; ?} ?public int getScore() { return score; ?} ?public void setScore(int score) { this score = score; ?} } public class manage { ?private people[] people_array ;// 對(duì)象數(shù)組 ?public void add_people(String uid String name) { String sql = "insert people (uid name) values ( " + uid + " " + name + " )";// sql插入語句 // String sql = "insert people (uid name) values ( uid tom )"; database_manage db_obj = new database_manage(); db_obj update_database(sql); ?} ?public void update_people(String uid String name) { String sql = "update people set name= " + name + " where uid= " + uid + " "; database_manage db_obj = new database_manage(); db_obj update_database(sql); ?} ?public void delete_people(String uid) { String sql = "delete from people? where uid= " + uid + " "; database_manage db_obj = new database_manage(); db_obj update_database(sql); ?} ?public people query_people(String uid) { database_manage db_obj = new database_manage(); // String adminid=null; String uid_new name banji; uid_new = null; name = null; banji = null; String sql_query = "select * from people where uid= " + uid + " "; try { ?ResultSet rs = db_obj query(sql_query); ?if (rs next()) { uid_new = rs getString("uid"); name = rs getString("name"); banji = rs getString("banji"); ?} } catch (Exception e) { ?e getMessage(); } people new_people = new people(uid_new name banji); return new_people; ?} ?public people[] query_people_byscore(int score) { database_manage db_obj = new database_manage(); String uid_new name banji; uid_new = null; name = null; banji = null; int score_new = ;
String sql_query = "select * from people where score=" + score;// sql查詢語句
try {
ResultSet rs = db_obj query(sql_query);// 查詢后 返回結(jié)果集
int num = ;
ResultSet rs_new = rs;
while (rs_new next()) {// 統(tǒng)計(jì)結(jié)果集中學(xué)生個(gè)數(shù)
num++;
}
// System out println(num);
people_array = new people[num];
int i = ;
rs beforeFirst();// 返回結(jié)果集的開始
while (rs next()) {
uid_new = rs getString("uid");
name = rs getString("name");
banji = rs getString("banji");
score_new = rs getInt("score");
people_array [i] = new people(uid_new name banji score_new);
i++;
}
} catch (Exception e) {
e getMessage();
}
return people_array ;
}
public static void main(String args[]) {
/*
* people new_people=new people();
*
* manage mr=new manage(); //mr add_people(" " "小明");插入一個(gè)學(xué)生的信息
*
* new_people=mr query_people(" ");//查詢uid= 的學(xué)生信息 返回對(duì)象 System out
* println(""+new_people getName()+" "+new_people getBanji());
* mr update_people(" " "小王");更新一個(gè)學(xué)生的信息
*
* new_people=mr query_people(" ");//更新后查詢
*
* System out println(""+new_people getName()+" "+new_people getBanji(
* ));
*/
manage mr = new manage();
// mr delete_people(" ");刪除uid= 的學(xué)生信息
people[] people_array;// 聲明對(duì)象數(shù)組
people_array = mr query_people_byscore( );// 返回成績(jī)?yōu)橐话俚膶W(xué)生類數(shù)組 后輸出
int num = ;
num = people_array length;
for (int i = ; i num; i++) {
System out println(people_array[i] getUid() + " "
+ people_array[i] getName() + " "
+ people_array[i] getBanji() + " "
+ people_array[i] getScore());
}
}
}
lishixinzhi/Article/program/Java/JSP/201311/20030
我就弄的用戶登入的代碼吧.這個(gè)挺簡(jiǎn)單的.
這是題目:
用戶登陸驗(yàn)證:
1.創(chuàng)建數(shù)據(jù)庫(kù)Test,并新建用戶表users
字段包含:username varchar(20) not null
userpwd varchar(20) not null
在JBUILDER中編寫Long類,實(shí)現(xiàn)登陸界面,并在用戶輸入用戶名和密碼后,
完成按紐的單擊事件,對(duì)用戶輸入的數(shù)據(jù)進(jìn)行驗(yàn)證,
(需要嚴(yán)整數(shù)據(jù)是否為空,密碼長(zhǎng)度必須是15位),
并實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的連接,將用戶輸入的用戶名密碼與表中的記錄比較,
若用戶名正確且密碼正確,彈出提示框告知登陸成功,否則登陸失敗。
這是代碼:
//連接數(shù)據(jù)庫(kù)
boolean isLogin(String name,String pwd){
boolean flag=false;
Connection conn=null;
PreparedStatement pst=null;
ResultSet rs=null;
//加載驅(qū)動(dòng)
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
//連接數(shù)據(jù)庫(kù)
try {
conn=DriverManager.getConnection("jdbc:odbc:login");
String sql="select * from [user] where username=? and userpwd=?";
pst=conn.prepareStatement(sql);
pst.setString(1,name);
pst.setString(2,pwd);
rs=pst.executeQuery();
if(rs.next())
flag=true;
} catch (Exception ex) {
ex.printStackTrace();
}finally{
try {
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return flag;
}
//驗(yàn)證方法
public void jButton1_actionPerformed(ActionEvent e) {
String name=jTextField1.getText();
String pwd=jTextField2.getText();
//錯(cuò)誤處理
if(name.equals("")||pwd.equals(""))
JOptionPane.showMessageDialog(this,"請(qǐng)輸入完整的信息");
else {
if(isLogin(name,pwd))
JOptionPane.showMessageDialog(this,"登陸成功");
else
JOptionPane.showMessageDialog(this,"用戶名或密碼錯(cuò)誤");
}
}
}
.....
.....
這是在事件里寫的,
你要連的數(shù)據(jù)庫(kù)是SQL 還是ORACLE
但是代碼都查不多
下面的是連接SQL數(shù)據(jù)庫(kù)的代碼
你需要先創(chuàng)建個(gè)數(shù)據(jù)庫(kù),還有表,表的字段是登陸名和密碼
下面的"SA" 是登陸名 "111111" 是密碼
ORACLE 和這個(gè)查不多
import java.sql.*;//做數(shù)據(jù)庫(kù)時(shí)候必須要引入的包
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
public class DBFactory {
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String OracleUserName="sa";
String OracleUserPwd="111111";
String ConnUrl="jdbc:sqlserver://localhost:1433;databaseName=news";
public Connection getConnection()
{
try {
Class.forName(driverName);
} catch (ClassNotFoundException ex) {
System.out.println("加載驅(qū)動(dòng)程序有錯(cuò)誤");
}
try {
Conn = DriverManager.getConnection(ConnUrl, OracleUserName,OracleUserPwd);
} catch (SQLException ex1) {
System.out.print("取得連接的時(shí)候有錯(cuò)誤,請(qǐng)核對(duì)用戶名和密碼");
}
return Conn;
}
這個(gè)是連接ORACLE數(shù)據(jù)庫(kù)代碼
import java.sql.*;
import oracle.jdbc.driver.OracleDriver;
public class DBFactory {
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
String driverName="oracle.jdbc.driver.OracleDriver";
String OracleUserName="scott";
String OracleUserPwd="tiger";
String ConnUr1="jdbc:oracle:thin:@locahost:1521:Ora";
public Connection getConnection()
{
try {
Class.forName(driverName);
} catch (ClassNotFoundException ex) {
System.out.println("加載驅(qū)動(dòng)程序有錯(cuò)誤");
}
try {
Conn = DriverManager.getConnection(ConnUr1, OracleUserName,OracleUserPwd);
} catch (SQLException ex1) {
System.out.print("取得連接時(shí)有錯(cuò)誤,請(qǐng)核對(duì)用戶名和密碼");
}
return Conn;
}
希望能追加分?jǐn)?shù)謝謝!
我用了半個(gè)小時(shí) 幫你寫了一個(gè)簡(jiǎn)單的驗(yàn)證用戶名和密碼登陸問題 別辜負(fù)我的好意 下面是代碼!(建好包和類 代碼粘過去就能用)
實(shí)體類 包entity
-------------------------------------------------------------
package entity;
/**
* 用戶實(shí)體類
* @author new
*
*/
public class Users {
private String name;//用戶名
private String pass;//用戶密碼
/**
* 空的構(gòu)造函數(shù) 用戶實(shí)力化 此類對(duì)象
*/
public Users(){
}
/**
* 構(gòu)造函數(shù) 接收用戶名和密碼
* @param name
* @param pass
*/
public Users(String name, String pass) {
this.name = name;
this.pass = pass;
}
/**
* 下面set和get方法就不用解釋了吧
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
數(shù)據(jù)庫(kù)類 包dao(我是模擬一下數(shù)據(jù)庫(kù) 沒有用到數(shù)據(jù)庫(kù))
--------------------------------------------------------------
package dao;
import java.util.*;
import entity.Users;//導(dǎo)入實(shí)體類
/**
* 模擬數(shù)據(jù)庫(kù) 用戶DAO
* @author new
*
*/
public class UsersDAO {
private static Users users=new Users();
static
{
users.setName("tom");
users.setPass("jerry");
}
/**
* 根據(jù)姓名查找這個(gè)用戶 (模擬一下數(shù)據(jù)庫(kù))
* @param name
* @return
*/
public Users findUserByName(String name)
{
if(name.equals(this.users.getName()))
{
return this.users;
}
return null;
}
}
業(yè)務(wù)類 包service (驗(yàn)證用戶名和密碼)
------------------------------------------------------------
package service;
import dao.UsersDAO;
import entity.Users;
/**
* 驗(yàn)證密碼 業(yè)務(wù)類
* @author new
*
*/
public class validatePass {
//實(shí)力化DAO對(duì)象
private UsersDAO us=new UsersDAO();
/**
* 驗(yàn)證輸入的密碼是否正確
* @param name
* @param pass
* @return
*/
public Users validate(String name,String pass)
{
Users user=null;
user=us.findUserByName(name);
//如果不為空 說明查到了
if(user!=null)
{
//用查詢出來對(duì)象的密碼和傳過來的密碼比較
if(user.getPass().equals(pass))
{
return user;
}
}
return null;
}
}
最后是測(cè)試test類 包test
----------------------------------------------------------
package test;
import entity.Users;
import service.validatePass;
/**
* 測(cè)試類
* @author new
*
*/
public class test {
/**
* main方法 用于測(cè)試
* @param args
*/
public static void main(String[] args)
{
//實(shí)例化業(yè)務(wù)類對(duì)象
validatePass v=new validatePass();
//用戶名和密碼
String name="tom";
String pass="jerry";
//開始驗(yàn)證
Users user=v.validate(name, pass);
if(user==null)
{
System.out.println("你輸入的用戶名或密碼錯(cuò)誤!");
}else
{
System.out.println("你已經(jīng)通過驗(yàn)證,成功登陸!");
}
}
}
package mysql;
import java.sql.*;
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("數(shù)據(jù)庫(kù)連接成功");
} else {
System.out.println("數(shù)據(jù)庫(kù)連接失敗");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 實(shí)例化 Statement 對(duì)象
Statement statement = getConnection().createStatement();
// 要執(zhí)行的 Mysql 數(shù)據(jù)庫(kù)操作語句(增、刪、改、查)
String sql = "";
// 展開結(jié)果集數(shù)據(jù)庫(kù)
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通過字段檢索
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 輸出數(shù)據(jù)
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
// 完成后需要依次關(guān)閉
resultSet.close();
statement.close();
getConnection().close();
}
}