数据库连接,包括语句 肯定没问题! 就是因为连接数据库实在是太慢了,我加进去个多线程,希望可以先报成功然后另一个进程去连接数据库,节约时间
import java.sql.*;
import java.util.Scanner;public class test2 { /**
* @param args
* jdbc方式操作数据库
* 需要引入三个jar
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
try {
//初始化各个对象
//1.加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.得到连接
ct=DriverManager.getConnection("jdbc:sqlserver://10.59.17.15;database=Test;integratedSecurity=true","sa","sa");
String choice="y";
Scanner in=new Scanner(System.in);
while (choice.equalsIgnoreCase("y")) {
System.out.println("请输入您的用户名");
String name=in.next();
System.out.println("请输入您的年龄");
int age=in.nextInt();
System.out.println("请输入您的成绩");
int score=in.nextInt();
MyThread MT=new MyThread(name, age, score,ps,ct,rs);
new Thread(MT).start();
System.out.println("录入成功,是否继续录入?");
choice=in.next();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭资源!!!!!!!顺序:后打开,先关闭
try{
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
}catch(SQLException e){
e.printStackTrace();
}finally{ }
} }
}class MyThread implements Runnable {
String name;
int age;
int score;
PreparedStatement ps;
Connection ct;
ResultSet rs; public MyThread(String Tname,int Tage,int Tscore,PreparedStatement Tps,Connection Tct,ResultSet Trs) {
name = Tname;
age=Tage;
score=Tscore;
PreparedStatement ps=Tps;
Connection ct=Tct;
ResultSet rs=Trs;
}
public void run() {
int i=0;
try {
ps=ct.prepareStatement("insert into S values( '"+name+"',"+age+","+score+")");
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} if(i==1){
System.out.println("成功!");
}else{
System.out.println("失败!");
}
}
}每次运行到ps=ct.prepareStatement("insert into S values( '"+name+"',"+age+","+score+")");都会报错Exception in thread "Thread-1" java.lang.NullPointerException
请大家抽点时间帮我看看...谢谢
import java.sql.*;
import java.util.Scanner;public class test2 { /**
* @param args
* jdbc方式操作数据库
* 需要引入三个jar
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
try {
//初始化各个对象
//1.加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.得到连接
ct=DriverManager.getConnection("jdbc:sqlserver://10.59.17.15;database=Test;integratedSecurity=true","sa","sa");
String choice="y";
Scanner in=new Scanner(System.in);
while (choice.equalsIgnoreCase("y")) {
System.out.println("请输入您的用户名");
String name=in.next();
System.out.println("请输入您的年龄");
int age=in.nextInt();
System.out.println("请输入您的成绩");
int score=in.nextInt();
MyThread MT=new MyThread(name, age, score,ps,ct,rs);
new Thread(MT).start();
System.out.println("录入成功,是否继续录入?");
choice=in.next();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭资源!!!!!!!顺序:后打开,先关闭
try{
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
}catch(SQLException e){
e.printStackTrace();
}finally{ }
} }
}class MyThread implements Runnable {
String name;
int age;
int score;
PreparedStatement ps;
Connection ct;
ResultSet rs; public MyThread(String Tname,int Tage,int Tscore,PreparedStatement Tps,Connection Tct,ResultSet Trs) {
name = Tname;
age=Tage;
score=Tscore;
PreparedStatement ps=Tps;
Connection ct=Tct;
ResultSet rs=Trs;
}
public void run() {
int i=0;
try {
ps=ct.prepareStatement("insert into S values( '"+name+"',"+age+","+score+")");
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} if(i==1){
System.out.println("成功!");
}else{
System.out.println("失败!");
}
}
}每次运行到ps=ct.prepareStatement("insert into S values( '"+name+"',"+age+","+score+")");都会报错Exception in thread "Thread-1" java.lang.NullPointerException
请大家抽点时间帮我看看...谢谢
解决方案 »
- (转)【解惑】这么多技术我该怎么学
- Java字体设置问题
- 打包怎么把数据库也打进去
- java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
- 请教JFrame 和 JPanel 的关系?
- 关于Applet一个问题,高手指教!!
- 请问线程的wait()和notify()具体是个什么用法???
- 关于URLConnection类的,视频终于学完了累呀。
- 在线高分等待高手相助(tomcat问题)
- 用java.util.random怎么得到一个指定区间的随机数
- java连接mysql问题
- finally段究竟有没有执行?
你看看参数传进来之前。也就是
ct=DriverManager.getConnection("jdbc:sqlserver://10.59.17.15;database=Test;integratedSecurity=true","sa","sa");
这里ct到底有没有获得哟?每日回帖,10分可用分~
name = Tname;
age=Tage;
score=Tscore;
PreparedStatement ps=Tps; // ??? 为何要定义?
Connection ct=Tct; // ??? 为何要定义?
ResultSet rs=Trs; // ??? 为何要定义?
}