java中登陆界面怎么连接到下一个界面啊_JAVA中界面如何连接数据库,如何实现一个界面到另一个界面...

展开全部

链接数据62616964757a686964616fe58685e5aeb931333332626631库代码最好封装成一个类,不然到处都写的是连接数据库的代码,用数据源链接数据库,代码如下:package Designer.FamilyAccountMoney.comm;import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.Properties;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;import Designer.FamilyAccountMoney.main.exception.ExceptionDemo;public class DBUtil {

private static final String driver;

private static final String url;

private static final String username;

private static final String password;

private static DataSource datasource;

private static HashMap map; static {

Properties config = new Properties();

InputStream in = DBUtil.class.getResourceAsStream("config.properties");

try {

config.load(in);

} catch (IOException e) {

throw new ExceptionDemo("装载配置文件出错",e);

}

driver = config.getProperty("jdbc.driver");

url = config.getProperty("jdbc.url");

username = config.getProperty("jdbc.username");

password = config.getProperty("jdbc.password");

map = new HashMap();

map.put("login.selectbynamepassword",config.getProperty("sql.login.selectbynamepassword"));

map.put("login.selectbyname",config.getProperty("sql.login.selectbyname"));

map.put("login.insert",config.getProperty("sql.login.insert"));

map.put("accountmoney.insert",config.getProperty("sql.accountmoney.insert"));

map.put("accountmoney.selectall",config.getProperty("sql.accountmoney.selectall"));

map.put("accountmoney.delete",config.getProperty("sql.accountmoney.delete"));

map.put("accountmoney.selectByDate",config.getProperty("sql.accountmoney.selectByDate"));

map.put("salary.selectall",config.getProperty("sql.salary.selectall"));

map.put("salary.selectbyname",config.getProperty("sql.salary.selectbyname"));

map.put("salary.delete",config.getProperty("sql.salary.delete"));

map.put("salary.insert",config.getProperty("sql.salary.insert"));

map.put("sort.selectall",config.getProperty("sql.sort.selectall"));

map.put("sort.selectbyname",config.getProperty("sql.sort.selectbyname"));

map.put("sort.delete",config.getProperty("sql.sort.delete"));

map.put("sort.insert",config.getProperty("sql.sort.insert"));

} public static DataSource getConnection() {

if(datasource==null){

BasicDataSource bds;

bds = new BasicDataSource();

bds.setDriverClassName(driver);

bds.setUrl(url);

bds.setUsername(username);

bds.setPassword(password);

datasource = bds;

}

return datasource; }

public static HashMap getMap() {

return map;

} // 释放资源

public static void realse(ResultSet rst, PreparedStatement psd, Connection conn) {

try {

if (rst != null) {

rst.close();

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (psd != null)

psd.close();

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (conn != null)

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

用法:例如判断用户名和密码:public static boolean Justice(String username, String password) {

try {

conn = dbs.getConnection();

psd = conn

.prepareStatement(map.get("login.selectbynamepassword"));

psd.setString(1, username);

psd.setString(2, password);

rst = psd.executeQuery();

flag = rst.next();

} catch (SQLException e) {

throw new ExceptionDemo("判断用户名和密码时出错", e);

} finally {

DBUtil.realse(rst, psd, conn);

}

if (flag) {

return true;

}

return false;

} 不过我给你的这个链接数据库要有配置文件,即:。property的文件:如下:jdbc.driver=sun.jdbc.odbc.JdbcOdbcDriver

jdbc.url=jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=stud.mdb

jdbc.username=

jdbc.password=

sql.login.selectbynamepassword=select user_name,pass_word from login where user_name=? and pass_word=?

sql.login.selectbyname=select username from login where username=?

sql.login.insert=insert into login(username,sex,age,user_name,pass_word) values(?,?,?,?,?) sql.accountmoney.insert=insert into account_money(in_out,sort_in_out,date_money,money_account,ps) values(?,?,?,?,?)

sql.accountmoney.selectall=select id,in_out,sort_in_out,date_money,money_account,ps from account_money

sql.accountmoney.selectByDate=select id,in_out,sort_in_out,date_money,money_account,ps from account_money where date_money between ? and ?

sql.accountmoney.delete=delete from account_money where id=?

sql.salary.selectall=select * from salary

sql.salary.selectbyname=select * from salary where in_sort=?

sql.salary.delete=delete from salary where in_sort=?

sql.salary.insert=insert into salary(in_sort) values(?)sql.sort.selectall=select * from sort

sql.sort.selectbyname=select * from sort where out_sort=?

sql.sort.delete=delete from sort where out_sort=?

sql.sort.insert=insert into sort(out_sort) values(?)不是很难,应该看的懂,上半部分是链接数据库必备条件,下面是执行的sql语句,在Dbutil里用静态代码快读出来。。这个是最底层的链接方式。。还有其他的第三方类库链接数据库。如:hibanate等等。。

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值