各位来看一下这段java连接数据库的代码
importjava.sql.*;publicclasstest()throwsSQLException{Connectionconn=null;Statementstm...
import java.sql.*;
public class test() throws SQLException
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String driver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft.sqlserver://localhost:1433;DatabaseName=Restaurant";
String user ="user";
String pwd ="user";
sql ="select name,password from operator;";
public static void main(String [] args)
{
Class.forName(driver);
System.out.println("加载驱动成功!");
conn = DriverManager.getConnection(url,user,pwd);
System.out.println("连接数据库成功!");
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println("------记录-------");
System.out.println("姓 名: "+rs.getString("name"));
System.out.println("密 码: "+rs.getString("password"));
}
}
}
编译时报错说:
public class test() throws SQLException
^
test.java:28: 需要 '}'
^
怎么回事啊?
sql语句多打了一个分号,实际上是没有的
报错说的是:
test.java:2: 需要 '{'
public class test() throws SQLException
^
test.java:28: 需要 '}'
2 错误 展开
public class test() throws SQLException
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String driver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft.sqlserver://localhost:1433;DatabaseName=Restaurant";
String user ="user";
String pwd ="user";
sql ="select name,password from operator;";
public static void main(String [] args)
{
Class.forName(driver);
System.out.println("加载驱动成功!");
conn = DriverManager.getConnection(url,user,pwd);
System.out.println("连接数据库成功!");
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println("------记录-------");
System.out.println("姓 名: "+rs.getString("name"));
System.out.println("密 码: "+rs.getString("password"));
}
}
}
编译时报错说:
public class test() throws SQLException
^
test.java:28: 需要 '}'
^
怎么回事啊?
sql语句多打了一个分号,实际上是没有的
报错说的是:
test.java:2: 需要 '{'
public class test() throws SQLException
^
test.java:28: 需要 '}'
2 错误 展开
展开全部
你这段程序错误太多了
1)
public class test() throws SQLException
test是类,不是方法,不要用(),不能抛出异常
改为
public class test
2)sql ="select name,password from operator;";
sql变量没有声明,改为
String sql ="select name,password from operator;";
3) main方法是静态方法,里面不能使用conn等非静态变量。
改为新定义一个方法,
然后再main中创建一个test对象,在调用该方法
4)因为除了SQLException,语句Class.forName(driver);还会抛出其它异常,所以在方法直接使用catch处理所有的异常。
修改后
import java.sql.*;
public class test
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String driver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft.sqlserver://localhost:1433;DatabaseName=Restaurant";
String user ="user";
String pwd ="user";
String sql ="select name,password from operator;";
public void doTest() {
try{
Class.forName(driver);
System.out.println("加载驱动成功!");
conn = DriverManager.getConnection(url,user,pwd);
System.out.println("连接数据库成功!");
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println("------记录-------");
System.out.println("姓 名: "+rs.getString("name"));
System.out.println("密 码: "+rs.getString("password"));
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String [] args)
{
new test().doTest();
}
}
1)
public class test() throws SQLException
test是类,不是方法,不要用(),不能抛出异常
改为
public class test
2)sql ="select name,password from operator;";
sql变量没有声明,改为
String sql ="select name,password from operator;";
3) main方法是静态方法,里面不能使用conn等非静态变量。
改为新定义一个方法,
然后再main中创建一个test对象,在调用该方法
4)因为除了SQLException,语句Class.forName(driver);还会抛出其它异常,所以在方法直接使用catch处理所有的异常。
修改后
import java.sql.*;
public class test
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String driver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft.sqlserver://localhost:1433;DatabaseName=Restaurant";
String user ="user";
String pwd ="user";
String sql ="select name,password from operator;";
public void doTest() {
try{
Class.forName(driver);
System.out.println("加载驱动成功!");
conn = DriverManager.getConnection(url,user,pwd);
System.out.println("连接数据库成功!");
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println("------记录-------");
System.out.println("姓 名: "+rs.getString("name"));
System.out.println("密 码: "+rs.getString("password"));
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String [] args)
{
new test().doTest();
}
}
展开全部
import java.sql.*;
public class test
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String driver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft.sqlserver://localhost:1433;DatabaseName=Restaurant";
String user ="user";
String pwd ="user";
String sql ="select name,password from operator;";
public void doTest() {
try{
Class.forName(driver);
System.out.println("加载驱动成功!");
conn = DriverManager.getConnection(url,user,pwd);
System.out.println("连接数据库成功!");
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println("------记录-------");
System.out.println("姓 名: "+rs.getString("name"));
System.out.println("密 码: "+rs.getString("password"));
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String [] args)
{
new test().doTest();
}
}
public class test
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String driver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft.sqlserver://localhost:1433;DatabaseName=Restaurant";
String user ="user";
String pwd ="user";
String sql ="select name,password from operator;";
public void doTest() {
try{
Class.forName(driver);
System.out.println("加载驱动成功!");
conn = DriverManager.getConnection(url,user,pwd);
System.out.println("连接数据库成功!");
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println("------记录-------");
System.out.println("姓 名: "+rs.getString("name"));
System.out.println("密 码: "+rs.getString("password"));
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String [] args)
{
new test().doTest();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询