写的实在太烂了。请拍砖,请指教。import java.sql.*;
public class JDBC {
public Connection con;
public Statement stmt;
JDBC()
{
}
public void Connection()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"sys as sysdba",
"welcome"
);
//myStatement = con.createStatement();
//myStatement.executeUpdate("insert into sys.CR_User values('03',2,2,'11-AUG-10','12-AUG-10')");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void execute( ){
try{
Statement myStatement = con.createStatement();
myStatement.executeUpdate("insert into CR_User values('3',1,2,'11-9月-10','11-9月-10')");
}catch(SQLException e){
System.out.println(e.getMessage());
} return;
}
public static void main(String args[])
{
// Statement myStatement;
//ResultSet rs;
for(int i = 0 ; i < 50 ;i++){
Orth t = new Orth(i);
new Thread(t).start();
}
}
}
class Orth extends Thread{
int itiao;
public Orth(int i){
itiao = i;
}
//Connection con ;
public void run()
{
while(true)
{
try{
JDBC jd = new JDBC();
jd.Connection();
jd.execute();
System.out.println("成功加入"+itiao);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
我现在只是想实现一个多线程访问数据库,所以没想什么连接池什么的。
本来jd.Connection()是写在main函数里的,是想只执行一次连接数据库。
但是这样的话在run()重无法用jd对象调用execute()了。
所以就都写在了run()方法里。
请大家拍砖 ,指教,或给出更好的例子。谢谢啦
public class JDBC {
public Connection con;
public Statement stmt;
JDBC()
{
}
public void Connection()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"sys as sysdba",
"welcome"
);
//myStatement = con.createStatement();
//myStatement.executeUpdate("insert into sys.CR_User values('03',2,2,'11-AUG-10','12-AUG-10')");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void execute( ){
try{
Statement myStatement = con.createStatement();
myStatement.executeUpdate("insert into CR_User values('3',1,2,'11-9月-10','11-9月-10')");
}catch(SQLException e){
System.out.println(e.getMessage());
} return;
}
public static void main(String args[])
{
// Statement myStatement;
//ResultSet rs;
for(int i = 0 ; i < 50 ;i++){
Orth t = new Orth(i);
new Thread(t).start();
}
}
}
class Orth extends Thread{
int itiao;
public Orth(int i){
itiao = i;
}
//Connection con ;
public void run()
{
while(true)
{
try{
JDBC jd = new JDBC();
jd.Connection();
jd.execute();
System.out.println("成功加入"+itiao);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
我现在只是想实现一个多线程访问数据库,所以没想什么连接池什么的。
本来jd.Connection()是写在main函数里的,是想只执行一次连接数据库。
但是这样的话在run()重无法用jd对象调用execute()了。
所以就都写在了run()方法里。
请大家拍砖 ,指教,或给出更好的例子。谢谢啦
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货