도구 류 개발 응용
JDBCTool
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBTools {
static{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");// mysql : "com.mysql.jdbc.Driver"
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getConn(){
String url ="jdbc:oracle:thin:@localhost:1521:XE"; //mysql : "jdbc:mysql://127.0.0.1/xxx" xxx
String dbname="hr";
String dbpwd="hr";
Connection conn = null;
try{
conn = DriverManager.getConnection(url,dbname,dbpwd);
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet rs,Statement stmt,Connection conn){
try{
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
테스트
package test;
import java.util.Date;
public class Emp {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private int sal;
private int comm;
private int deptno;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
}
package test;
import java.util.List;
/**
* @author Administrator
*
*/
public interface EmpDAO {
public void addEmp(Emp emp);
public void delEmp(int empno);
public void udpateEmp(Emp emp);
public Emp findEmpById(int empno);
public List<Emp> findEmps();
}
package test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import util.DBTools;
public class EmpDAOImpl implements EmpDAO {
public void addEmp(Emp emp) {
String sql = "insert into emp values(?,?,?,?,?,?,?,?)";
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = DBTools.getConn();
stmt = conn.prepareStatement(sql);
stmt.setInt(1,emp.getEmpno());
stmt.setString(2,emp.getEname());
stmt.setString(3,emp.getJob());
stmt.setInt(4,emp.getMgr());
stmt.setDate(5,new java.sql.Date(emp.getHiredate().getTime()));
stmt.setInt(6,emp.getSal());
stmt.setInt(7,emp.getComm());
stmt.setInt(8,emp.getDeptno());
int temp = stmt.executeUpdate();
if(temp != 0){
System.out.println(" !");
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBTools.close(null,stmt,conn);
}
}
public void delEmp(int empno) {
String sql = "delete emp where empno=?";
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = DBTools.getConn();
stmt = conn.prepareStatement(sql);
stmt.setInt(1,empno);
int temp = stmt.executeUpdate();
if(temp != 0){
System.out.println(" !");
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBTools.close(null,stmt,conn);
}
}
public Emp findEmpById(int empno) {
String sql = "select * from emp where empno=?";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet temp=null;
Emp emp=new Emp();
try{
conn = DBTools.getConn();
stmt = conn.prepareStatement(sql);
stmt.setInt(1,empno);
temp = stmt.executeQuery();
while(temp.next()){
emp.setEmpno(temp.getInt(1));
emp.setEname(temp.getString(2));
emp.setJob(temp.getString(3));
emp.setMgr(temp.getInt(4));
emp.setHiredate(temp.getDate(5));
emp.setSal(temp.getInt(6));
emp.setComm(temp.getInt(7));
emp.setDeptno(temp.getInt(8));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBTools.close(temp,stmt,conn);
}
return emp;
}
public List<Emp> findEmps() {
String sql = "select * from emp";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet temp=null;
List<Emp> list=new ArrayList<Emp>();
try{
conn = DBTools.getConn();
stmt = conn.prepareStatement(sql);
temp = stmt.executeQuery();
while(temp.next()){
Emp emp=new Emp();
emp.setEmpno(temp.getInt(1));
emp.setEname(temp.getString(2));
emp.setJob(temp.getString(3));
emp.setMgr(temp.getInt(4));
emp.setHiredate(temp.getDate(5));
emp.setSal(temp.getInt(6));
emp.setComm(temp.getInt(7));
emp.setDeptno(temp.getInt(8));
list.add(emp);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBTools.close(temp,stmt,conn);
}
return list;
}
public void udpateEmp(Emp emp) {
//String sql = "update emp set ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=? where empno=?";
String sql = "update emp set ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=? where empno=?";
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = DBTools.getConn();
stmt = conn.prepareStatement(sql);
stmt.setString(1,emp.getEname());
stmt.setString(2,emp.getJob());
stmt.setInt(3,emp.getMgr());
stmt.setDate(4,new java.sql.Date(emp.getHiredate().getTime()));
stmt.setInt(5,emp.getSal());
stmt.setInt(6,emp.getComm());
stmt.setInt(7,emp.getDeptno());
stmt.setInt(8,emp.getEmpno());
int temp = stmt.executeUpdate();
if(temp != 0){
System.out.println(" !");
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBTools.close(null,stmt,conn);
}
}
public static void main(String[] args){
//===========================
//Emp emp = new Emp();
//emp.setComm(10);
//emp.setDeptno(10);
//emp.setEmpno(7839);
//emp.setEname("kaka");
//emp.setHiredate(new Date());
//emp.setJob("clark");
//emp.setMgr(101);
//emp.setSal(1000);
//EmpDAOImpl st = new EmpDAOImpl();
//st.addEmp(emp);
//==========================
//EmpDAOImpl st = new EmpDAOImpl();
//st.delEmp(emp.getEmpno());
//===========================
//EmpDAOImpl st = new EmpDAOImpl();
//Emp emp= st.findEmpById(7499);
//System.out.println(" :"+emp.getEmpno()+" :"+emp.getEname()
// +" :"+emp.getJob()+" :"+emp.getMgr()+" :"+
// emp.getHiredate()+" :"+emp.getSal()+" "+emp.getComm()+" :"+emp.getDeptno());
//=============================
//EmpDAOImpl st = new EmpDAOImpl();
//List<Emp> list= st.findEmps();
//for(Emp e:list){
//System.out.println(e.getEmpno()+" "+e.getEname()+" "+e.getJob()
// +" "+e.getMgr()+" "+e.getHiredate()+" "+e.getSal()
// +" "+e.getComm()+" "+e.getDeptno());
// }
//==========================
Emp emp = new Emp();
emp.setEmpno(7839);
emp.setEname("kaka");
emp.setJob("clark");
emp.setMgr(101);
emp.setHiredate(new Date());
emp.setSal(1000);
emp.setComm(100);
emp.setDeptno(10);
EmpDAOImpl st = new EmpDAOImpl();
st.udpateEmp(emp);
}
}
HibernateUtil
package cn.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static Configuration config = null;
private static SessionFactory factory = null;
static {
// config hibernate.cfg.xml
// , ..
// static
try {
config = new Configuration().configure();
factory = config.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static SessionFactory getFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void close(Session session) {
if (session != null) {
session.close();
}
}
public static void close(SessionFactory sf) {
if (sf != null) {
sf.close();
}
}
}
package cn.java;
public class User {
private int id;
private String name;
private int age;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="cn.java">
<class name="User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1/test
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="cn/java/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
package test;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.java.User;
import cn.util.HibernateUtil;
public class TestUser extends TestCase {
// ;
// test
//
public void testhibernate() {
Configuration config = null;
SessionFactory factory = null;
Session session = null;
Transaction tx = null;
try {
// : ;
// hibernate.cfg.xml ;
// ,,
// hiberante
// , ,pas,name,url
config = new Configuration().configure();
// =======
// sessionfactory
//
// session
// factory
factory = config.buildSessionFactory();
// session
// session jdbc connection
// save
// get
// del
// update
// find
session = factory.openSession();
//
// : sql ;;
// jdbc;
// ...
// hibernate
tx = session.beginTransaction();
for (int i = 0; i < 5; i++) {
User user = new User();
user.setAge(13);
user.setName("jerry"+i);
//
session.save(user);
}
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
} finally {
if (session != null) {
session.close();
}
if (factory != null) {
factory.close();
}
}
}
public void testHibernate21() {
Session session = null;
//Transaction tx = null;
try {
//session = HibernateUtil.getSession();
//tx = session.beginTransaction();
session = HibernateUtil.getSession();
session.beginTransaction();
User user = new User();
user.setAge(19);
user.setName("kaka");
session.save(user);
session.getTransaction().commit();
//tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.close(session);
}
}
}
hibenate 주석 개발
package cn.Util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernatAannotationUtil {
private final static SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable e) {
throw new ExceptionInInitializerError(e);
}
}
public static Session getSession() {
return sessionFactory.openSession();
}
public static void close(Session session) {
if (session != null) {
session.close();
}
}
}
spring 도구 클래스
package cn.java;
public class Room {
private int id;
private String name;
public Room() {
System.out.println(" room ");
}
public Room(int id){
this.id = id;
}
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;
public class User {
private int id;
private String name;
private Room room;
public User() {
}
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
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;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-
2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="u1" class="cn.java.User">
<property name="id" value="1"/>
<property name="name" value="tom"/>
<property name="room">
<ref bean="room"/>
</property>
</bean>
<bean id="room" class="cn.java.Room" >
<property name="name" value="011" />
</bean>
</beans>
package cn.java;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class UserUtil extends TestCase{
public void test(){
// spring;
// , ;;
//User u = new User();
//u.setId(1);
//u.setName("tom");
//User u1 = new User();
//=========================
//spring ../
// spring ;
// User spring ;;;
//spring
//applicatoinContext.xml
//php 1 3
//java 1 3
//ClassPathXmlApplicationContext(); class
//FileSystemXmlApplicationContext("c://applicationContext.xml");
//WebApplicationContextUtils;
ApplicationContext ctx = null;
//ClassPathXmlApplicationContext
// xml;
// , ;;
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
User u1 = (User)ctx.getBean("u1");
//User u2 = (User)ctx.getBean("u1");
//System.out.println(u1 == u2);
//System.out.println(u1.getId()+u1.getName());
System.out.println(u1.getRoom().getName());
}
}
비동기 전송
function createXMLHttpRequest() {
var objXMLHttp = null;
// Mozilla、Firefox、Opera
try {
objXMLHttp = new XMLHttpRequest();
}
// Internet Explorer
catch (e) {
var MSXML = [ 'MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ];
for ( var n = 0; n < MSXML.length; n++) {
try {
// XMLHttpRequest
objXMLHttp = new ActiveXObject(MSXML[n]);
break;
} catch (e) {
}
}
}
return objXMLHttp;
}
//xml.open("get", "checkUser.jsp?name=" + username,true);
function checkUser(user) {
var username = user.value;
var xml = createXMLHttpRequest();
xml.open("post", "checkUser.jsp",true);
xml.setRequestHeader("Content-Type"
, "application/x-www-form-urlencoded");
xml.onreadystatechange = function() {
//alert("readyState:"+xml.readyState+"\t\r"+"status:"+xml.status);
if (xml.readyState == 4) {
if (xml.status == 200 || xml.status == 304) {
alert(xml.responseText);
}
}
}
xml.send("name="+username);
}
// literal :XMLHttp
var XMLHttp =
{
// , XMLHttpRequest
XMLHttpRequestPool: [],
// , XMLHttpRequest
getInstance:function()
{
// XMLHttpRequest XMLHttpRequest
for (var i = 0; i < this.XMLHttpRequestPool.length; i ++)
{
// XMLHttpReuqest readyState 0, 4,
// XMLHttpRequest
if (this.XMLHttpRequestPool[i].readyState == 0 ||
this.XMLHttpRequestPool[i].readyState == 4)
{
return this.XMLHttpRequestPool[i];
}
}
// , XMLHttpRequest
this.XMLHttpRequestPool[this.XMLHttpRequestPool.length]
= this.createXMLHttpRequest();
// XMLHttpRequest
return this.XMLHttpRequestPool[this.XMLHttpRequestPool.length - 1];
},
// XMLHttpRequest
createXMLHttpRequest:function()
{
// DOM 2
if (window.XMLHttpRequest)
{
var objXMLHttp = new XMLHttpRequest();
}
// Internet Explorer
else
{
// Internet Explorer XMLHTTP ActiveX
var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
// Internet Explorer XMLHTTP , XMLHttpRequest
for(var n = 0; n < MSXML.length; n ++)
{
try
{
// XMLHttpRequest , break
var objXMLHttp = new ActiveXObject(MSXML[n]);
break;
}
catch(e)
{
}
}
}
//Mozilla readyState
if (objXMLHttp.readyState == null)
{
// readyState 0
objXMLHttp.readyState = 0;
// readyState , load
objXMLHttp.addEventListener("load", function ()
{
// , readyState 4
objXMLHttp.readyState = 4;
if (typeof objXMLHttp.onreadystatechange == "function")
{
objXMLHttp.onreadystatechange();
}
}, false);
}
return objXMLHttp;
},
// : ( [POST,GET], , , )
sendRequest: function (method, url, data, callback)
{
var objXMLHttp = this.getInstance();
with(objXMLHttp)
{
try
{
// randnum , IE
if (url.indexOf("?") > 0)
{
url += "&randnum=" + Math.random();
}
else
{
url += "?randnum=" + Math.random();
}
//
open(method, url, true);
// POST
if (method == "POST")
{
//
setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
send(data);
}
// GET
if (method == "GET")
{
send(null);
}
//
onreadystatechange = function ()
{
// ,
if (objXMLHttp.readyState == 4 &&
(objXMLHttp.status == 200 ||
objXMLHttp.status == 304))
{
// ,
callback(objXMLHttp);
}
}
}
catch(e)
{
alert(e);
}
}
}
};
//
XMLHttp.sendRequest("Post",url,data,callback);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.