当前位置: 技术问答>java相关
出错了,谁来帮帮我?
来源: 互联网 发布时间:2015-06-02
本文导语: 我的程序代码如下: import java.sql.*; import java.io.*; public class Database { Connection con; Statement stmt; ResultSet result; String sqlstr; String name,sex,pass; try { //connect to database Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInst...
我的程序代码如下:
import java.sql.*;
import java.io.*;
public class Database
{
Connection con;
Statement stmt;
ResultSet result;
String sqlstr;
String name,sex,pass;
try
{ //connect to database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
con = DriverManager.getConnection("jdbc:odbc:bbs","","");
stmt = con.createStatement();
sqlstr="insert into user (username,usersex,pass) values ('"+name+"','"+sex+"','"+pass+"')";
result = stmt.executeQuery(sqlstr);
}catch(Exception e)
{
System.out.println("error:"+e.toString()+"here");
}
}
运行javac Database.java时出现以下错误:
Database.java:13: illegal start of type
try
Database.java:24: expected
}
2 errors
怎么解决?
import java.sql.*;
import java.io.*;
public class Database
{
Connection con;
Statement stmt;
ResultSet result;
String sqlstr;
String name,sex,pass;
try
{ //connect to database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
con = DriverManager.getConnection("jdbc:odbc:bbs","","");
stmt = con.createStatement();
sqlstr="insert into user (username,usersex,pass) values ('"+name+"','"+sex+"','"+pass+"')";
result = stmt.executeQuery(sqlstr);
}catch(Exception e)
{
System.out.println("error:"+e.toString()+"here");
}
}
运行javac Database.java时出现以下错误:
Database.java:13: illegal start of type
try
Database.java:24: expected
}
2 errors
怎么解决?
|
import java.sql.*;
import java.io.*;
public class Database
{
public Database(){
Connection con;
Statement stmt;
ResultSet result;
String sqlstr;
String name,sex,pass;
try
{ //connect to database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
con = DriverManager.getConnection("jdbc:odbc:bbs","","");
stmt = con.createStatement();
sqlstr="insert into user (username,usersex,pass) values ('"+name+"','"+sex+"','"+pass+"')";
result = stmt.executeQuery(sqlstr);
}catch(Exception e)
{
System.out.println("error:"+e.toString()+"here");
}
}
}
import java.io.*;
public class Database
{
public Database(){
Connection con;
Statement stmt;
ResultSet result;
String sqlstr;
String name,sex,pass;
try
{ //connect to database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
con = DriverManager.getConnection("jdbc:odbc:bbs","","");
stmt = con.createStatement();
sqlstr="insert into user (username,usersex,pass) values ('"+name+"','"+sex+"','"+pass+"')";
result = stmt.executeQuery(sqlstr);
}catch(Exception e)
{
System.out.println("error:"+e.toString()+"here");
}
}
}
|
在类里面要定义函数,在函数里实现你的方法的具体代码
不能在类里面直接写try后面那些代码,而是要写到一个函数里
不能在类里面直接写try后面那些代码,而是要写到一个函数里