import javasql;
/-
Copyright(C) >
首先创建一个连接工厂import javasqlConnection;
import javasqlDriverManager;
import javasqlResultSet;
import javasqlStatement;public class ConnectionFactory {
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
public ConnectionFactory() {
super();
// TODO Auto-generated constructor stub
} public void OpenConn() throws Exception{
try{
ClassforName("commysqljdbcDriver")newInstance();
String url="jdbc:mysql://127001:3306/guestbook";
String user="root";
String password="root";
conn=DriverManagergetConnection(url,user,password);
}catch(Exception e){
Systemoutprintln("创建链接抛出异常为:"+egetMessage());
}
} public ResultSet executeQuery(String sql) throws Exception{
try{
stmt=conncreateStatement(ResultSetTYPE_SCROLL_SENSITIVE,ResultSetCONCUR_UPDATABLE);
rs=stmtexecuteQuery(sql);
}catch(Exception e){
Systemoutprintln("执行查询抛出的异常为:"+egetMessage());
}
return rs;
} public void close() throws Exception{
try{
rsclose();
stmtclose();
connclose();
}catch(Exception e){
Systemoutprintln("关闭对象抛出的异常:"+egetMessage());
}
} }
测试类 import javasqlResultSet;public class TestJDBC {
public static void main(String[] args) {
ConnectionFactory c= new ConnectionFactory();
try {
cOpenConn();
String sql="select from tb_guestbook";
ResultSet rs=cexecuteQuery(sql);
while(rsnext()){
Systemoutprintln(rsgetString(2));
}
cclose();
Systemoutprintln();
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
String url = "jdbc:mysql://localhost:3306/bbsdb";
String username = "root";
String password = "pla";
ClassforName("commysqljdbcDriver")newInstance();
conn = DriverManagergetConnection(url, username, password);
区别在于url里面的localhost,这个是本机,要是访问其他电脑,把这里localhost换成那个机器的IP地址,然后mysql的账号密码也要用那个电脑的mysql账号密码
以上的代码都不如哥的 且看哥是怎么写条理清晰的代码的!!!
package dbconnection //java 中不存在没有包的类(讲解详细因为项目需要)
import javasqlConnection;
import javasqlDriverManager;
import javasqlSQLException;
import javasqlStatement;//引入sql数据库包
public class DBConnection{
private Connection conn=null;
private Statement stmt=null;
private Result rs=null;
private String jdbc="commicrosoftsqlserverjdbcSQLServerDriver";
private String driverManager="jdbc:sqlserver://localhost:1433;databasename=HcitPos";
private String user="admin";
private int password="admin";
public DBConnection{
try{
ClassforName("jdbc");
conngetConnection("driverManager");
}
catch(Exception e){}
}
public selectMethod(String sql){
stmt=conncreateStatement();
rs=stmtextcuteQuery("sql");
while(rsnext()){
String title=rsgetString("title");//利用javaBean获得数据库中的属性
String name=rsgetString("name");
Systemoutprintln("title");
Systemoutprintln("name");
//当然数据库的 *** 作有很多 这里简单介绍下功能的实现
}
}
public void closeDB(){
if(rs != null) rsclose();
if(stmt != null) stmtclose();
if(conn != null) connclose();
}
}
显示加载jdbc驱动,类;classforName("commysqljdbcDriver");
通过DriverManagergetNecction(url,name,password);获取连接对象
之后通过conncreateStatement();获取 *** 作sql的对象statement;
然后就可以执行sql语句(update,query)statementexecuteQuery(sql)或者statementexecuteQuery(sql);来执行查询 *** 作
1增加
String s1="insert into tableNames (id,name,password) values(myseqnextval,,);"
ClassforName(driver);
Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);
PreparedStatement prepStmt = connprepareStatement(s1);
prepStmtsetString(1,name);
prepStmtsetString(2,password);
ResultSet rs=stmtexecuteUpdate();
2、删除
String s2="delete from tbNames where name=";
ClassforName(driver);
Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);
PreparedStatement prepStmt = connprepareStatement(s2);
prepStmtsetString(1,name);
ResultSet rs=stmtexecuteUpdate();
3、修改
String s3=“update tbNames set name= where id=”;
ClassforName(driver);
Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);
PreparedStatement prepStmt = connprepareStatement(s3);
prepStmtsetString(1,name);
prepStmtsetString(2,id);
ResultSet rs=stmtexecuteUpdate();
4、查询
String s4="select id,name,password from tbNames";
ClassforName(driver);
Connection conn = DriverManagergetConnection(url,dbUser,dbPwd);
Statement stmt=conncreateStatement();
ResultSet rs = stmtexecuteQuery(s4);
while(rsnext){
int id=rsgetInt(1);
String name = rsgetString(2);
String pwd=rsgetString(3);
Systemoutprintln(id+name+pwd); }
以上四步必须都得关闭连接;!!!
rsclose();
stmtclose();
connclose();
以上就是关于java连接数据库的方法,最好有详细的代码全部的内容,包括:java连接数据库的方法,最好有详细的代码、java连接数据库具体 *** 作以及代码!最好有个例子 越详细越好、java链接MYSQL数据库并实现查找数据代码如何写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)