初学白小黑寻“高手”!(JDBC问题)java 连接数据库出错!(全代码)
public class Access_CreateStudent{
public static void main(String args[]){
String url = "jdbc:odbc:mydata";
String sql = "create table studentnum"+
"(name VARCHAR(20)," +
"sex CHAR(2),"+
"birthday Date,"+
"leave BIT,"+
"stnumber INTEGER)";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(
java.lang.ClassNotFoundException e){
System.out.println("出现数据库连接错误! class
NotFoundException !");
e.printStackTrace();
}
try{
Connection con = DriverManager.getConnection(url,null,null);
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
System.out.println("student table created");
stmt.close();
con.close();
}catch(
SQLException sqle){
System.out.println("出现数据库连接错误!");
sqle.printStackTrace();
}
}
}
编译可以,但是运行就出错了,还请高手们赐教!白小黑拜谢!
------解决方案--------------------Access_CreateStudent.main(Access_CreateStudent.java:19)
19行是哪行,自己看看
------解决方案--------------------Connection con = DriverManager.getConnection(url,null,null);
你三个参数一个都不传? 这个conn 也能new 出来 ?
------解决方案--------------------"jdbc:odbc:mydata";
应该是这里错了,是不是mydata,确认一下
------解决方案--------------------好吧,我是给你粘贴的实在不先打字了~~好好看看,我刚开始学习的时候也是这样的,慢慢的多写几遍就好了~
一、JDBC连接DB2
Class.forName("Com.ibm.db2.jdbc.net.DB2Driver");
String url="jdbc:db2://dburl:port/DBname"
cn = DriverManager.getConnection( url, sUsr, sPwd );
二、JDBC连接Microsoft SQLServer(microsoft)
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
cn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://DBServerIP:1433;databaseName=master", sUsr, sPwd );
三、JDBC连接Sybase(jconn2.jar)
Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );
cn = DriverManager.getConnection( "jdbc:sybase:Tds:DBServerIP:2638", sUsr, sPwd );
四、JDBC连接MySQL(mm.mysql-3.0.2-bin.jar)
Class.forName( "org.gjt.mm.mysql.Driver" );
cn = DriverManager.getConnection( "jdbc:mysql://DBServerIP:3306/myDatabaseName", sUsr, sPwd );
五、JDBC连接PostgreSQL(pgjdbc2.jar)
Class.forName( "org.postgresql.Driver" );
cn = DriverManager.getConnection( "jdbc:postgresql://DBServerIP/myDatabaseName", sUsr, sPwd );
六、JDBC连接Oracle(classes12.jar)
Class.forName( "oracle.jdbc.driver.OracleDriver" );
cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
七、JDBC连接ODBC
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Connection cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd );