JAVA中界面如何连接数据库,如何实现一个界面到另一个界面
2个回答
2013-08-01
展开全部
链接数据库代码最好封装成一个类,不然到处都写的是连接数据库的代码,用数据源链接数据库,代码如下: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<String,String> 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<String, String>();
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<String,String> 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等等。。
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<String,String> 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<String, String>();
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<String,String> 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等等。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询