java的jdbc编程_Java - 面向对象思想进行JDBC编程

mysql-connector-java-5.1.7-bin.jar

jdbc.propertiesdriver=com.mysql.jdbc.Driver

jdbcUrl=jdbc\:mysql\://localhost\:3306/lesson

user=root

password=

JDBCTools.javapackage 面向对象思想进行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.javapublic 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.javaimport 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编程

标签:pack   resource   XML   pre   etag   rom   1.7   input   access

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:http://www.cnblogs.com/anstoner/p/6506569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值