日期:2014-05-17 浏览次数:21016 次
package db; import java.sql.Connection; import java.sql.DriverManager; public class DBcon { private String url; // 存储SQLSever连接路径 private String serverName; // 存储机器的名称 private String portNumber; // 存储端口名称 private String databaseName; // 存储数据库名称 private String userName; // 存储用户名称 private String password; // 存储密码 /* 设置连接数据库相关参数 */ public DBcon(){ url = "jdbc:jtds:sqlserver://"; serverName = "localhost"; portNumber = "1433"; databaseName = "JSP_Test"; userName = "sa"; password = ""; } /* 获取连接数据库路径并返回 */ private String getConnectionUrl() { return url + serverName + ":" + portNumber + ";databaseName=" + databaseName + ";"; } /* 获取Conncetion对象并返回 */ public Connection getConnection() { Connection con=null; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); // 加载Jdbc驱动程序 con = DriverManager.getConnection(getConnectionUrl(),userName,password); } catch (Exception e) { e.printStackTrace(); System.out.println("getConnection()内部跟踪错误:"+ e.getMessage()); } return con; } }
package impl; import pojo.Student; import db.DBcon; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class StudentImpl { public void saveStudent(Student student) { DBcon conn = new DBcon(); Connection con = null; PreparedStatement pre = null; con = conn.getConnection(); try { pre = con.prepareStatement("insert into t31_Student(sname,slikes) values('?','?')"); pre.setString(1, student.getSname()); pre.setString(2, student.getSadress()); pre.executeUpdate(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("StudentImpl"); System.out.println(student.getSname()); System.out.println(student.getSadress()); } }