myeclipse怎么连接 sql数据库 怎么驱动?

来源:百度知道 编辑:UC知道 时间:2024/09/19 16:25:32
package edu.xs2242.zhangke;

import java.sql.*;
public class NewsFirstTitleDB {
public static void main(String[] args){
Connection con = null;
Statement statement = null;
try{
String strSql = "insert into FirstLevelTiele values(1,'军事','网管',getdate())";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){
System.out.println("无法找到驱动类");
}
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=news","sa","sa");
statement = con.createStatement();
statement.executeUpdate(strSql);
}catch(SQLException sqlE){
sqlE.printStackTrace();
}finally{
closeStatement(statement);
closeConnection(con);
}
}
public static void closeStatement(Statement stat){
try{
if(stat != n

要导入SQL server的驱动包,你可以去网上找了下载,放到lib目录里。
还有,在NewsFirstTitleDB.java中第15行有错误。

sql语句里最后一个参数getdate()不正确。不能用方法或参数代替变量

导入包