麻烦高手
我的jsp老是连不上数据库呢,sql都配置好了,sqljdbc.jar也有了,环境变量也配置了,但老是连不上。
代码是用的一下的:
public class ConnectionDemo
{
public ConnectionDemo()
{
}
void display()
{
Connection con=null;
String url="jdbc:sqlserver://localhost:1433;DatabaseName=MyDB";
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("成功加载驱动!");
String username="sa";
String password="123";
con=DriverManager.getConnection(url,username,password);
System.out.println("成功连接");
Statement stmt=con.createStatement();
int id;
String text;
int age;
ResultSet rs;
String strSQL="Select* From Person";
rs=stmt.executeQuery(strSQL);
if(rs.next())
{
do{
id=rs.getInt(1);
System.out.print("ID:"+id);
text=rs.getString(2);
System.out.print("name"+text);
text=rs.getString(3);
System.out.print("sex"+text);
age=rs.getInt(4);
System.out.print("age"+age);
text=rs.getString("addresss");
System.out.print("address:"+text);
text=rs.getString("phone");
System.out.print("phone:"+text);
}
while(rs.next());
}
else{
System.out.println("no data");
}
}
catch(ClassNotFoundException e){
System.out.println(e);
}
catch(SQLException e){
System.out.println(e);
}
finally{
try{
if(con!=null){
con.close();
}
}
catch(SQLException e){
System.out.println(e);
}
}
}
public static void main(String args[]){
ConnectionDemo app=new ConnectionDemo();
app.display();
}
}
我的jsp老是连不上数据库呢,sql都配置好了,sqljdbc.jar也有了,环境变量也配置了,但老是连不上。
代码是用的一下的:
public class ConnectionDemo
{
public ConnectionDemo()
{
}
void display()
{
Connection con=null;
String url="jdbc:sqlserver://localhost:1433;DatabaseName=MyDB";
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("成功加载驱动!");
String username="sa";
String password="123";
con=DriverManager.getConnection(url,username,password);
System.out.println("成功连接");
Statement stmt=con.createStatement();
int id;
String text;
int age;
ResultSet rs;
String strSQL="Select* From Person";
rs=stmt.executeQuery(strSQL);
if(rs.next())
{
do{
id=rs.getInt(1);
System.out.print("ID:"+id);
text=rs.getString(2);
System.out.print("name"+text);
text=rs.getString(3);
System.out.print("sex"+text);
age=rs.getInt(4);
System.out.print("age"+age);
text=rs.getString("addresss");
System.out.print("address:"+text);
text=rs.getString("phone");
System.out.print("phone:"+text);
}
while(rs.next());
}
else{
System.out.println("no data");
}
}
catch(ClassNotFoundException e){
System.out.println(e);
}
catch(SQLException e){
System.out.println(e);
}
finally{
try{
if(con!=null){
con.close();
}
}
catch(SQLException e){
System.out.println(e);
}
}
}
public static void main(String args[]){
ConnectionDemo app=new ConnectionDemo();
app.display();
}
}
1.报什么错
2.String strSQL="Select* From Person"; // String strSQL="Select * From Person 空格
3.if(rs.next())
{
do{
id=rs.getInt(1);
System.out.print("ID:"+id);
text=rs.getString(2);
System.out.print("name"+text);
text=rs.getString(3);
System.out.print("sex"+text);
age=rs.getInt(4);
System.out.print("age"+age);
text=rs.getString("addresss");
System.out.print("address:"+text);
text=rs.getString("phone");
System.out.print("phone:"+text);
}
while(rs.next());
}
else{
System.out.println("no data");
}用不着这么写
while(rs.next()){
id=rs.getInt(1);
System.out.print("ID:"+id);
....
}
明显是找不到jar包的错误,web项目的话你在tomcat里的webapps下
找到你项目名称,然后WEB-INF,然后在lib文件夹里放那个jar包
你这个是连接2000的数据库
com.microsoft.sqlserver.jdbc.SQLServerDriver
这个才是连接2005的数据库
while(rs.next()){
System.out.println(rs.getInt(1));
}
试验下