package cn.java.jdbc;
import java.io.InputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Configuration {
private String url;
private String driver;
private String username;
private String password;
public Configuration() {
}
public Configuration(String url, String driver, String username,
String password) {
super();
this.url = url;
this.driver = driver;
this.username = username;
this.password = password;
}
public static Configuration getConfigure()
{
try {
InputStream in = Configuration.class.getResourceAsStream("/db.xml");
if (null!=in) {
return load(in);
}
return null;
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
private static Configuration load(InputStream in) throws DocumentException {
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
Element jdbc = doc.getRootElement();
String url = jdbc.element("url").getText();
String driver = jdbc.element("driver").getText();
String username = jdbc.element("username").getText();
String password = jdbc.element("password").getText();
Configuration cfg = new Configuration(url, driver, username, password);
return cfg;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?xml version="1.0" encoding="UTF-8"?> <jdbc> <url>jdbc:mysql://localhost:3306/test</url> <driver>com.mysql.jdbc.Driver</driver> <username>root</username> <password></password> </jdbc>
package cn.java.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
private static ConnectionFactory connectionFactory=null;
private static Configuration config=Configuration.getConfigure();
private ConnectionFactory()
{
try {
Class.forName(config.getDriver());
} catch (ClassNotFoundException e) {
}
}
public Connection getConnection() throws SQLException
{
Connection con=null;
try {
con=DriverManager.getConnection(config.getUrl(), config.getUsername(), config.getPassword());
return con;
}finally{
// if (null != con) {
// con.close();
// }
}
}
public static ConnectionFactory getInstance()
{
if (null==connectionFactory) {
connectionFactory=new ConnectionFactory();
}
return connectionFactory;
}
}
package cn.java.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class ConnectionFactory2 {
private DataSource ds ;
private static ConnectionFactory2 connectionFactory2 = null;
private ConnectionFactory2() {
MysqlDataSource myDS = new MysqlDataSource() ;
myDS.setServerName("localhost");
myDS.setDatabaseName("test");
myDS.setPort(3306) ;
myDS.setUser("root");
myDS.setCharacterEncoding("utf-8");
myDS.setPassword("");
this.ds = myDS ;
}
public Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = ds.getConnection() ;
conn.setAutoCommit(false);
return conn;
} catch (SQLException e) {
if (null != conn) {
conn.close();
}
throw e;
}
}
public static ConnectionFactory2 getInstance() {
if (null == connectionFactory2) {
connectionFactory2 = new ConnectionFactory2();
}
return connectionFactory2;
}
}
package cn.java.jdbc;
public class User {
private int id;
private String name;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package cn.java.jdbc;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
private PreparedStatement st = null;
private ResultSet rs = null;
public UserDao() {
}
public void insert( Connection con,String name) throws SQLException,IOException {
String sql="insert into user(name) values(?) ";
try{
st=con.prepareStatement(sql);
st.setString(1, name);
st.executeUpdate();
}finally{
if (null!=st) {
st.close();
}
}
}
public void delete(Connection con,int id) throws SQLException,IOException {
String sql="delete from user where id=?";
try{
st=con.prepareStatement(sql);
st.setInt(1, id);
st.executeUpdate();
}finally{
if (null!=st) {
st.close();
}
}
}
public void update( Connection con,int id,String name) throws SQLException,IOException {
String sql="update user set name=? where id=?";
try{
st=con.prepareStatement(sql);
st.setString(1, name);
st.setInt(2, id);
st.executeUpdate();
}finally{
if (null!=st) {
st.close();
}
}
}
public User query(Connection con,int index) throws SQLException,IOException{
User user=new User();
String sql="select * from user where id=?";
try{
st=con.prepareStatement(sql);
st.setInt(1, index);
rs=st.executeQuery();
while (rs.next()) {
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
break;
}
}finally{
if (null!=rs) {
rs.close();
}
if (null!=st) {
st.close();
}
}
return user;
}
public List<User> queryAll(Connection con) throws SQLException,IOException {
List<User> list=new ArrayList<>();
String sql="select * from user";
try {
st=con.prepareStatement(sql);
rs=st.executeQuery();
while (rs.next()) {
User user=new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
list.add(user);
}
}finally{
if (null!=rs) {
rs.close();
}
if (null!=st) {
st.close();
}
}
return list;
}
}
package cn.java.jdbc;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public class TestJdbc {
public static void main(String[] args) {
Connection con=null;
try {
con=(Connection) ConnectionFactory.getInstance().getConnection();
UserDao userDao=new UserDao();
//con=(Connection) ConnectionFactory2.getInstance().getConnection();
if (null!=con) {
System.out.println("Link JDBC SUCESS");
//userDao.insert(con, "zhangsir");
//userDao.delete(con, 4);
//userDao.update(con, 1, "david");
List<User> list=userDao.queryAll(con);
for (User user : list) {
System.out.println("id="+user.getId()+" name="+user.getName());
}
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (null!=con) {
try {
con.close();
} catch (SQLException e) {
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有