当前位置: 代码迷 >> Java相关 >> 如何连接数据库
  详细解决方案

如何连接数据库

热度:268   发布时间:2006-11-03 15:58:51.0
如何连接数据库


import java.sql.*;
public class shujuku {

public shujuku() {
}

public static void main(String[] args) {
// TODO code application logic here
String JDriver="sun.jdbc.odbc.jdbcOdbcDriver";
String conURL="jdbc:odbc:TestDB";
try
{
Class.forName(JDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("ForName:"+e.getMessage());
}
try
{
Connection con=DriverManager.getConnection(conURL);
Statement s=con.createStatement();
String query="creat table zg("
+"职工号 char(10),"
+"姓名 char(15),"
+"性别 char(15),"
+"工资 integer,"
+"职称 char(15)"
+")";
s.executeUpdate(query);
String r1="insert into zg values("+"'1002','张小华','男','600','助工')";
String r2="insert into zg values("+"'1007','李梨','女','1000','工程师')";
String r3="insert into zg values("+"'1001','丁为国','男','650','助工')";
String r4="insert into zg values("+"'1005','黄菊','女','1200','工程师')";
String r5="insert into zg values("+"'1003','宁涛','男','2500','高工')";
s.executeUpdate(r1);
s.executeUpdate(r2);
s.executeUpdate(r3);
s.executeUpdate(r4);
s.executeUpdate(r5);
s.close();
con.close();
}
catch(SQLException e)
{
System.out.println("SQLException:"+e.getMessage());
}
}

}


运行后:
init:
deps-jar:
Compiling 1 source file to E:\java\数据库\shujuku\build\classes
compile:
run:
ForName:sun.jdbc.odbc.jdbcOdbcDriver
SQLException:No suitable driver
生成成功(总时间:0 秒)


这个问题要怎么解决~ 急
还有我已经建立了testDB 这个数据源

搜索更多相关的解决方案: 数据库  

----------------解决方案--------------------------------------------------------
没有找到你的数据库驱动
你的J小写了
sun.jdbc.odbc.jdbcOdbcDriver
应该改为大写
----------------解决方案--------------------------------------------------------
非常感谢啊 ~
----------------解决方案--------------------------------------------------------
厉害!!!



----------------解决方案--------------------------------------------------------
String query="creat table zg("
是create,你也写错了!!!

----------------解决方案--------------------------------------------------------
  相关解决方案