import java.sql.*
public class DBConnectionManager {
private String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
private String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName=demo"
private String user="sa"
private String password=""
public String getDriverName() {
return driverName
}
public void setDriverName(String driverName) {
this.driverName = driverName
}
public String getPassword() {
return password
}
public void setPassword(String password) {
this.password = password
}
public String getUrl() {
return url
}
public void setUrl(String url) {
this.url = url
}
public String getUser() {
return user
}
public void setUser(String user) {
this.user = user
}
public Connection getConnection(){
try{
Class.forName(driverName)
return DriverManager.getConnection(url, user, password)
}catch(Exception e){
e.printStackTrace()
return null
}
}
}
DBSQLManager.java
import java.sql.*
public class DBSQLManager {
protected Connection con=null//Connection对象
protected Statement stmt=null//Statement对象
protected ResultSet rs=null//记录结果集
protected String sql=""//SQL语句
public DBSQLManager(){
try {
DBConnectionManager dcm=new DBConnectionManager()
con=dcm.getConnection()
//con.setAutoCommit(false)//添加事物,既是否自动提交
stmt=con.createStatement()
} catch (SQLException e) {
e.printStackTrace()
}
}
public Statement getStmt(){
return stmt
}
public Connection getCon(){
return con
}
public ResultSet getRs(){
return rs
}
public void setSql(String sql){
this.sql=sql
}
public String getSql(){
return sql
}
//查找
public void execueQuery(){
try {
rs=stmt.executeQuery(sql)
} catch (SQLException e) {
e.printStackTrace()
}
}
//更新
public void executeUpdate(){
try {
stmt.executeUpdate(sql)
} catch (SQLException e) {
e.printStackTrace()
}
}
//关闭
public void close(){
if(rs!=null){
try {
rs.close()
} catch (SQLException e) {
e.printStackTrace()
}
rs=null
}
if(stmt!=null){
try {
stmt.close()
} catch (SQLException e) {
e.printStackTrace()
}
stmt=null
}
try {
con.close()
} catch (SQLException e) {
e.printStackTrace()
}
con=null
}
}
如果楼主有不明白的,,百度HI我
祝楼主早日成功!
Class.forName("com.mysql.jdbc.Driver")Connection conn=DriverManager.getConnection("jdbc:mysql://"+host+"/"+dbname,username,password)
Statement Stmt=conn.createStatement()
ResultSet rs=Stmt.executeQuery(sql)
这是基本的数据库 *** 作方法
大概流程就是加载驱动类,创建连接,执行数据库 *** 作,关闭
首先我觉得你的问题不太明确,做增删改查,的话一般不用ajax,除非其中要用到单独的验证字段的时候采用,比如在注册时验证用户名,这里用到ajax查询用户名是否存在,返回给页面直接用流打回页面就行(比如:此用户名可用之类的)而其他的查询比如显示所有或者查询的结果为对象的话那看你用什么框架(controller),struts直接封装到值栈中,在页面用标签显示就行,不知道能不能帮到你欢迎分享,转载请注明来源:内存溢出
评论列表(0条)