importjava.sql.*;publicclassXsglxt{publicstaticvoidmain(String[]args){PreparedStatementps=null;Connectionct=null;ResultSetrs=null;try{Class.forName("sun.jdbc.odbc.JdbcOdb...
import java.sql.*;
public class Xsglxt {
public static void main(String[] args){
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ct =DriverManager.getConnection("jdbc:odbc:sql server","sa","621513");
ps=ct.prepareStatement("create table shuju(aa int,bb nvarchar(2))");//添加表
ps.execute();
System.out.println("执行完毕");
} catch (Exception e){}
finally
{
try {
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
if(ct!=null)
{
ct.close();
}
} catch (Exception e){}
}
}
}
上面代码执行后什么都没有显示,数据库也没有多出一张表格,数据源之类的也已经配置好了,可就是连接不上,读取不了sql中的数据。我sql登陆的服务器名称用的是localhost,不知道跟这个有没有关系
自己试了N次试出来的:sql2005的连接代码为这个,并且java要添加sqljdbc.jar这个包
Connection ct=null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=填入数据库名","sa","登陆密码");
展开