package zmsjdbc;
import java.sql.*;
public class db
{
//连接mssql数据库
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xuzhoujob";
//catv是数据库!loacalhost可以用IP和主机
private String user="sa";
private String pwd="123456";
//连接mysql数据库
private String mysqlurl="jdbc:mysql:localhost:3306/xuzhoujob";
private String mysqluser="root";
private String mysqlpwd="root";
//获取sqlsever连接
public Connection getcon()
{
Connection con;
try
{ //加载驱动程序
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//创建连接
con=DriverManager.getConnection(url,user,pwd);
return con;
}
catch(ClassNotFoundException e)
{
System.out.println("加载sqlserver驱动程序出错");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("出现错误了,我也不知道是啥错误,来自sqlsever数据库");
} return null;
}
//连接mysql数据库
public Connection getconmysql()
{
Connection conmysql;
try
{ //加载驱动程序
Class.forName("com.mysql.jdbc.Driver").newInstance();
//创建连接
conmysql=DriverManager.getConnection(mysqlurl,mysqluser,mysqlpwd);
return conmysql;
}
catch(ClassNotFoundException e)
{
System.out.println("加载mysql驱动程序出错");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("出现错误了,我也不知道是啥错误,来自mysql");
} return null;
}
public ResultSet executeSql(String sqlstr)
{
Connection conn;
Statement stmt; try
{
conn=getcon();
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sqlstr); return rs;
}
catch(SQLException e)
{
System.out.print("获取数据集时出现错误");
}
return null;
}
public int inserttomysql(String sql){
Connection conn;
Statement stmt;
try
{
conn=getconmysql();
stmt=conn.createStatement();
int rs=stmt.executeUpdate(sql);
return rs;
}
catch(SQLException e)
{
System.out.print("插入数据集时出现错误");
}
return 1;
}
public static void main(String[] args) throws SQLException
{
ResultSet rs1;
db sql1=new db();
rs1=sql1.executeSql("select * from pH_New_Info where NewId='1'");
String title=(String)rs1.getString("title");
String insql="insert into pH_New_Info(title) values('title')";
try
{
while(rs1.next())
{
System.out.println(rs1.getString("ui_id")+" 姓名:"+rs1.getString("ui_name"));
sql1.inserttomysql(insql);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}报错如下:
加载sqlserver驱动程序出错
Exception in thread "main" java.lang.NullPointerException
at zmsjdbc.db.executeSql(db.java:74)
at zmsjdbc.db.main(db.java:105)
import java.sql.*;
public class db
{
//连接mssql数据库
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xuzhoujob";
//catv是数据库!loacalhost可以用IP和主机
private String user="sa";
private String pwd="123456";
//连接mysql数据库
private String mysqlurl="jdbc:mysql:localhost:3306/xuzhoujob";
private String mysqluser="root";
private String mysqlpwd="root";
//获取sqlsever连接
public Connection getcon()
{
Connection con;
try
{ //加载驱动程序
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//创建连接
con=DriverManager.getConnection(url,user,pwd);
return con;
}
catch(ClassNotFoundException e)
{
System.out.println("加载sqlserver驱动程序出错");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("出现错误了,我也不知道是啥错误,来自sqlsever数据库");
} return null;
}
//连接mysql数据库
public Connection getconmysql()
{
Connection conmysql;
try
{ //加载驱动程序
Class.forName("com.mysql.jdbc.Driver").newInstance();
//创建连接
conmysql=DriverManager.getConnection(mysqlurl,mysqluser,mysqlpwd);
return conmysql;
}
catch(ClassNotFoundException e)
{
System.out.println("加载mysql驱动程序出错");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("出现错误了,我也不知道是啥错误,来自mysql");
} return null;
}
public ResultSet executeSql(String sqlstr)
{
Connection conn;
Statement stmt; try
{
conn=getcon();
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sqlstr); return rs;
}
catch(SQLException e)
{
System.out.print("获取数据集时出现错误");
}
return null;
}
public int inserttomysql(String sql){
Connection conn;
Statement stmt;
try
{
conn=getconmysql();
stmt=conn.createStatement();
int rs=stmt.executeUpdate(sql);
return rs;
}
catch(SQLException e)
{
System.out.print("插入数据集时出现错误");
}
return 1;
}
public static void main(String[] args) throws SQLException
{
ResultSet rs1;
db sql1=new db();
rs1=sql1.executeSql("select * from pH_New_Info where NewId='1'");
String title=(String)rs1.getString("title");
String insql="insert into pH_New_Info(title) values('title')";
try
{
while(rs1.next())
{
System.out.println(rs1.getString("ui_id")+" 姓名:"+rs1.getString("ui_name"));
sql1.inserttomysql(insql);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}报错如下:
加载sqlserver驱动程序出错
Exception in thread "main" java.lang.NullPointerException
at zmsjdbc.db.executeSql(db.java:74)
at zmsjdbc.db.main(db.java:105)
2.Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 好像不需要newInstance()直接Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")就可以吧
改为:
private String url="jdbc:sqlserver://localhost:1433;DatabaseName=xuzhoujob";
试试
另如二楼所说 代码中没必要直接用newInstance()做到下面两点就不会报楼主所说的异常:
1、驱动版本正确 因为sql2005 的驱动包有几种哦 (总之吧url和驱动名写正确就是了)
比如:下面是另一个版本的sql2005驱动
public static String jdbcUrl = "jdbc:jtds:sqlserver://10.0.0.10:1433/GZSeachDB;user=sa;password=sa01";
Class.forName("net.sourceforge.jtds.jdbc.Driver");
2、保证代码能加载到驱动的jar包,即jar包要放到程序能够引用到的位置
"jdbc:sqlserver://127.0.0.1:1433;DatabaseName=数据库名称","数据库用户","密码"这样就可以了,如果还不行,你装sql server 2000的补丁了吗?如果没有那赶紧装吧,最好装SP4的。
访问或修改 null 对象的字段。
将 null 作为一个数组,获得其长度。
将 null 作为一个数组,访问或修改其时间片。
将 null 作为 Throwable 值抛出。
应用程序应该抛出该类的实例,指示其他对 null 对象的非法使用。