mysql编码规范_mysql标准代码

import java.sql.*;

import java.util.ArrayList;

import java.util.Date;

public class MysqlDemo {

public static String url = "jdbc:mysql://localhost:3306/test";

public static String user = "root";

public static String password = "root";

public static String driver = "com.mysql.jdbc.Driver";

public static void main(String[] args) {

String sql = "select * from test.emp ";

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

ArrayList entities = new ArrayList();

try {

Class.forName(driver);

conn = DriverManager.getConnection(url,user,password);

conn.setAutoCommit(false);

ps = conn.prepareStatement(sql);

rs= ps.executeQuery();

while (rs.next()){

Entity entity = new Entity();

entity.setEmpno( rs.getString("empno") );

entity.setEname( rs.getString("ename") );

entity.setJob( rs.getString("job"));

entity.setSal( rs.getDouble("sal"));

entity.setHiredate( new Date(rs.getDate("hiredate").getTime()) );

entities.add(entity);

}

conn.commit();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}finally {

try {

if (rs != null) {

rs.close();

}

if (ps != null){

ps.close();

}

if (conn != null){

conn.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

for(Entity temp:entities){

System.out.println(temp);

}

}

public static class Entity{

public String empno;

public String ename;

public String job;

public Date hiredate;

public double sal;

@Override

public String toString() {

return "Entity{" +

"empno='" + empno + '\'' +

", ename='" + ename + '\'' +

", job='" + job + '\'' +

", hiredate=" + hiredate +

", sal=" + sal +

'}';

}

public String getEmpno() {

return empno;

}

public void setEmpno(String 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 Date getHiredate() {

return hiredate;

}

public void setHiredate(Date hiredate) {

this.hiredate = hiredate;

}

public double getSal() {

return sal;

}

public void setSal(double sal) {

this.sal = sal;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值