Java - 物件導向思想進行JDBC編程

來源:互聯網
上載者:User

標籤:pack   resource   XML   pre   etag   rom   1.7   input   access   

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 blocke.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 blocke.printStackTrace();}finally{if(conn!=null)try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.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();}@Overridepublic 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 blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

Java - 物件導向思想進行JDBC編程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.