首页 > 数据库技术 > 详细

mysql jdbc

时间:2017-03-26 21:20:54      阅读:224      评论:0      收藏:0      [点我收藏+]

技术分享

 

技术分享

package cj;

public class Stu {
private int Id;
private String Name;

private String Age;
private String birth;
private String phone;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public Stu(int id, String name, String age) {
super();
Id = id;
Name = name;
Age = age;
}
public String getName() {
return Name;
}
public Stu(int id, String name, String age, String birth, String phone) {
super();
Id = id;
Name = name;
Age = age;
this.birth = birth;
this.phone = phone;
}
public void setName(String name) {
Name = name;
}
public String getAge() {
return Age;
}
public void setAge(String age) {
Age = age;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}

}

package cj;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class Conn {
private static Connection getConn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/stu";
String username = "root";
String password = "1";
Connection conn = null;
try {
Class.forName(driver); //classLoader,加载对应驱动
conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
private static int insert(Stu student) {
Connection conn=getConn();
int i = 1;
String sql = "insert into s1(id,name,Age,phone,birth) values(?,?,?,?,?)";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setLong(1, student.getId());
pstmt.setString(2, student.getName());

pstmt.setString(3, student.getAge());
pstmt.setString(4, student.getPhone());

pstmt.setString(5, student.getBirth());

i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}

private static int update(Stu student) {
Connection conn = getConn();
int i = 0;
String sql = "update s1 set Age=‘" + student.getAge() + "‘ where id=‘" + student.getName() + "‘";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println("resutl: " + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}

private static Integer getAll() {
Connection conn = getConn();
String sql = "select * from s1";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement)conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();
System.out.println("============================");
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + "\t");
if ((i == 2) && (rs.getString(i).length() < 8)) {
System.out.print("\t");
}
}
System.out.println("");
}
System.out.println("============================");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

private static int delete(String name) {
Connection conn = getConn();
int i = 0;
String sql = "delete from s1 where Name=‘" + name + "‘";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println("resutl: " + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
public static void main(String[] args) {
Conn.getAll();
Conn.insert(new Stu(4, "Male", "14","18888888888","201710"));
Conn.getAll();
Conn.update(new Stu(3, "Male", "7"));
Conn.delete("Achilles");
Conn.getAll();
}
}

 

mysql jdbc

原文:http://www.cnblogs.com/zxl1/p/6623968.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!