
Statement stmt = conn.createStatementI()
String sql = "CREATE TABLE PFO_ANALYSE_BRANCH ( "
+" NODE_NAME_S VARCHAR2(50 BYTE), "
+ 其他字段
+")"
stmt.execute(sql)
import java.sql.*
public class Test
{
public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver")
//一开始必须填一个已经存在的数据库
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"
Connection conn = DriverManager.getConnection(url, "root", "123456")
Statement stat = conn.createStatement()
//创建数据库hello
stat.executeUpdate("create database hello")
//打开创建的数据库
stat.close()
conn.close()
url = "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8"
conn = DriverManager.getConnection(url, "root", "123456")
stat = conn.createStatement()
//创建表test
stat.executeUpdate("create table test(id int, name varchar(80))")
//添加数据
stat.executeUpdate("insert into test values(1, '张三')")
stat.executeUpdate("insert into test values(2, '李四')")
//查询数据
ResultSet result = stat.executeQuery("select * from test")
while (result.next())
{
System.out.println(result.getInt("id") + " " + result.getString("name"))
}
//关闭数据库
result.close()
stat.close()
conn.close()
}
}
实体类
package daopublic class Person {
private Integer personid
private String personname
private String degree
private String birth
private Integer sal
public Person() {
}
public Person(Integer personid, String personname, String degree, String birth, Integer sal) {
super()
this.personid = personid
this.personname = personname
this.degree = degree
this.birth = birth
this.sal = sal
}
public Integer getPersonid() {
return personid
}
public void setPersonid(Integer personid) {
this.personid = personid
}
public String getPersonname() {
return personname
}
public void setPersonname(String personname) {
this.personname = personname
}
public String getDegree() {
return degree
}
public void setDegree(String degree) {
this.degree = degree
}
public String getBirth() {
return birth
}
public void setBirth(String birth) {
this.birth = birth
}
public Integer getSal() {
return sal
}
public void setSal(Integer sal) {
this.sal = sal
}
@Override
public String toString() {
return "Person [personid=" + personid + ", personname=" + personname + ", degree=" + degree + ", birth=" + birth
+ ", sal=" + sal + "]"
}
}
2. dao
package daoimport java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.util.ArrayList
import java.util.List
public class PersonDao {
static Connection conn = null
static {
// 修改成自己的数据库
String url = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useSSL=false"
String user = "root"
String password = "root"
try {
// 记得导入 驱动包
// 驱动名称:com.mysql.jdbc.Driver (指的是驱动版本:5.7版本的)
// com.mysql.cj.jdbc.Driver (8.0版本)
Class.forName("com.mysql.jdbc.Driver")
conn = DriverManager.getConnection(url, user, password)
} catch (SQLException e) {
// TODO Auto-generated catch block
conn = null
e.printStackTrace()
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
conn = null
e.printStackTrace()
}
}
/**
* 查找所有的数据
* @return List集合
* @throws SQLException
*/
public List<Person> findAll() throws SQLException{
List<Person> list = null
if(conn != null) {
list = new ArrayList<>()
String sql = "select * from person"
PreparedStatement ps = conn.prepareStatement(sql)
ResultSet rs = ps.executeQuery()
while(rs.next()) {
Person temp = new Person()
temp.setPersonid(rs.getInt("personid"))
temp.setPersonname(rs.getString("personname"))
temp.setDegree(rs.getString("degree"))
temp.setBirth(rs.getString("birth"))
temp.setSal(rs.getInt("sal"))
list.add(temp)
}
}
return list
}
public Person findById(int personId) throws SQLException {
Person temp = null
if(conn != null){
String sql = "select * from person where personid =?"
PreparedStatement ps = conn.prepareStatement(sql)
ps.setInt(1, personId)
ResultSet rs = ps.executeQuery()
while(rs.next()) {
temp = new Person()
temp.setPersonid(rs.getInt("personid"))
temp.setPersonname(rs.getString("personname"))
temp.setDegree(rs.getString("degree"))
temp.setBirth(rs.getString("birth"))
temp.setSal(rs.getInt("sal"))
}
}
return temp
}
/**
* 通过id来删除一条记录
* @param personId
* @throws SQLException
*/
public void deleteById(int personId) throws SQLException {
if(conn != null) {
String sql = "delete from person where personid = ?"
PreparedStatement ps = conn.prepareStatement(sql)
ps.setInt(1, personId)
ps.execute()
}else {
System.out.println("无法建立连接")
}
}
// 其他的可以自己试试来写!模板都是一样的,除了sql语句不同
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)