首页 > 代码库 > Java - 面向对象思想进行JDBC编程
Java - 面向对象思想进行JDBC编程
mysql-connector-java-5.1.7-bin.jar
jdbc.properties
driver=com.mysql.jdbc.DriverjdbcUrl=jdbc\:mysql\://localhost\:3306/lessonuser=rootpassword=
JDBCTools.java
package 面向对象思想进行JDBC编程;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties;import com.mysql.jdbc.Driver;import com.mysql.jdbc.Statement;public class JDBCTools { public static void update(String sql) { Connection conn=null; java.sql.Statement statement=null; try {conn=getConnection(); statement= conn.createStatement(); statement.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally {JDBCTools.release(conn, (Statement) statement); } } public static Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException { String driverClass=null; String jdbcUrl=null; String user=null; String password=null; InputStream in= JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties p=new Properties(); p.load(in); driverClass=p.getProperty("driver"); jdbcUrl=p.getProperty("jdbcUrl"); user=p.getProperty("user"); password=p.getProperty("password"); Driver driver=(Driver) Class.forName(driverClass).newInstance(); Properties info=new Properties (); info.put("user",user); info.put("password",password); Connection connection=driver.connect(jdbcUrl, info); System.out.println(connection); return connection; } public static void release(Connection conn,Statement statement) { try { if(statement!=null) statement.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
Student.java
public class Student { private int id; private String name; private int age; private int clas; private String address; private String sex; 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; } public int getClas() { return clas; } public void setClas(int clas) { this.clas = clas; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Student(int id, String name, int age, int clas, String address, String sex) { super(); this.id = id; this.name = name; this.age = age; this.clas = clas; this.address = address; this.sex = sex; } public Student() { super(); } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", clas=" + clas + ", address=" + address + ", sex=" + sex + "]"; } }
test.java
import java.io.IOException;import java.sql.SQLException;import java.util.Scanner;public class test {// public void testAddnewStudent() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException// {// // testAddNew(test.getInformationFromConsole());// } private static Student getInformationFromConsole() { Scanner sc=new Scanner(System.in); Student stu=new Student(); System.out.println("Id:"); stu.setId(sc.nextInt()); System.out.println("Name:"); stu.setName(sc.next()); System.out.println("Age:"); stu.setAge(sc.nextInt()); System.out.println("Clas:"); stu.setClas(sc.nextInt()); System.out.println("Address:"); stu.setAddress(sc.next()); System.out.println("Sex:"); stu.setSex(sc.next()); return stu; } public void testAddNew(Student stu) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException{ String sql="INSERT INTO study VALUES("+stu.getId()+",‘"+stu.getName()+"‘,"+stu.getAge()+","+stu.getClas()+",‘"+stu.getAddress()+"‘,‘"+stu.getSex()+"‘)"; System.out.println(sql); JDBCTools.update(sql); } public static void main(String[] args) { test te=new test(); try { te.testAddNew(te.getInformationFromConsole()); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Java - 面向对象思想进行JDBC编程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。