标签:
package com.hei.jdbcdemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");
} catch (SQLException e) {
e.printStackTrace();
}
String sql="select id,name from student";
try {
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
System.out.println("id:"+id+",name:"+name);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
Properties prop = new Properties();
Reader in = new FileReader("src\\config.propertions");
prop.load(in);
driver=prop.getProperty("driver");
url=prop.getProperty("url");
username=prop.getProperty("username");
password=prop.getProperty("password");
public class Test2 {
public static void main(String[] args) {
//createTable();
//insert();
//updata();
//delete();
query();
}
static void createTable(){
Connection conn = DBUtil.open();
String sql = "create table UserTbl(id int primary key auto_increment,name varchar(20))";
try {
Statement stmt = conn.createStatement();
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
static void insert(){
Connection conn = DBUtil.open();
String sql = "insert UserTbl Values(NULL,‘Tom‘)";
try {
Statement stat = conn.createStatement();
stat.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
static void updata(){
Connection conn = DBUtil.open();
String sql = "update UserTbl set name=‘BigTom‘ where id>3";
try {
Statement stat = conn.createStatement();
stat.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
static void delete(){
Connection conn = DBUtil.open();
String sql = "delete from UserTbl where id=1";
try {
Statement stat = conn.createStatement();
stat.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
static List<User> query(){
Connection conn = DBUtil.open();
String sql = "select id,name from UserTbl";
List<User> list= new ArrayList<User>();
try{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
User u = new User();
u.setId(id);
u.setName(name);
list.add(u);
//System.out.println("id"+id+" name"+name);
}
System.out.println(list);
}catch(Exception e){
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
return list;
}
}
static void insert(String name,int id){
String sql = "insert into UserTbl(id,name)values(?,?)";
Connection conn = DBUtil.open();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,id);
pstmt.setString(2, name);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
public static List<Student> select(){
Connection conn=DBUtil.open();
List<Student> list = new ArrayList<Student>();
String sql ="select * from Stud";
try {
Statement stat =conn.createStatement();
ResultSet rs = stat.executeQuery(sql);//结果集
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String sex = rs.getString(4);
Date date = rs.getDate(5);
Student student=new Student();
student.setAge(age);
student.setDate(date);
student.setId(id);
student.setName(name);
student.setSex(sex);
list.add(student);
}
for (Student student : list) {
System.out.println(student);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} return list;
}
标签:
原文地址:http://www.cnblogs.com/songwenyi/p/5656111.html