import java.sql.*;
class SqlTest{
public static void main(String[] args){
Connection con=null;
Statement stat=null;
ResultSet rs=null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
}
catch(Exception e){
System.out.println("Can't find");}
String url="jdbc:microsoft:sqlserver://MIRCOSOF-1B612C:1433;DatabaseName=pubs";
String user="";
String password="";
try{
con=DriverManager.getConnection(url,user,password);
}
catch(SQLException e){
System.out.println("Can't connection");}
try {
stat = con.createStatement();
rs = stat.executeQuery( "select * from jobs");
while (rs.next()) {
System.out.println(rs.getString("job_id") + rs.getString("job_desc"));
}
}
catch(SQLException sqlex) {
System.out.println("警告:数据不完整");
} finally{
if (con!= null) {
try {
con.close();
}
catch(SQLException sqlex2) {
}
} }
}
}
为什么总是输出Can't find,既然驱动已加载成功,那还有什么问题???
class SqlTest{
public static void main(String[] args){
Connection con=null;
Statement stat=null;
ResultSet rs=null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
}
catch(Exception e){
System.out.println("Can't find");}
String url="jdbc:microsoft:sqlserver://MIRCOSOF-1B612C:1433;DatabaseName=pubs";
String user="";
String password="";
try{
con=DriverManager.getConnection(url,user,password);
}
catch(SQLException e){
System.out.println("Can't connection");}
try {
stat = con.createStatement();
rs = stat.executeQuery( "select * from jobs");
while (rs.next()) {
System.out.println(rs.getString("job_id") + rs.getString("job_desc"));
}
}
catch(SQLException sqlex) {
System.out.println("警告:数据不完整");
} finally{
if (con!= null) {
try {
con.close();
}
catch(SQLException sqlex2) {
}
} }
}
}
为什么总是输出Can't find,既然驱动已加载成功,那还有什么问题???
----------谁告诉你驱动已经加载成功了
你直接输出e看看,
System.out.println("Can't find");}你这么写任何异常都会输出can't find
import java.sql.SQLException
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class CreatTableTest
{
private Connection con;
private String url;//数据库URL
private String user;//数据库用户名
private String password;//用户密码
public static void main(String[] args)
{
CreatTableTest test=new CreatTableTest();
test.getProperty();
Connection con=test.getConnection();
test.createTable(con);
test.getStudent(con);
}
public void createTable(Connection con){
try
{
Statement st=con.createStatement();
String sql="CREATE TABLE student (姓名 varchar(12) NOT NULL,学号 varchar(10) NOT NULL,出生日期 datetime NOT NULL,专业 varchar(10)NULL,籍贯 varchar(255))";
System.out.println("输入的SQL的语句是:");
System.out.println(sql);
st.execute(sql);
sql="insert into student values('张丽','20054012','女','1978-6-10','英语','山东')";
st.executeUpdate(sql);
st.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
/*
* 从表中读出学生记录
*/
public void getStudent(Connection con)
{
try
{
Statement st=con.createStatement();
String sql="select * from student";
ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
String name=rs.getString("姓名");
String number=rs.getString("学号");
String sex=rs.getString("性别");
String time=rs.getString("出生日期");
String spe=rs.getString("专业");
String address=rs.getString("籍贯");
System.out.println("/n姓名:"+name+" 学号:"+number+"性别:"+sex+
" 出生日期:"+time+" 专业:"+spe+" 籍贯:"+address);
st.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
/*
*
*/
public Connection getConnection()
{
try
{
con=DriverManager.getConnection(url,user,password);
}
catch(SQLException e)
{
e.printStackTrace();
}
System.out.println("连上数据库");
return con;
}
/*
* 读取配置文件
*/
public void getProperty()
{
Properties prop=new Properties();
try
{
FileInputStream in=new FileInputStream("driver.properties");
prop.load(in);
String drivers=prop.getProperty("drivers");
System.setProperty("jdbc.drivers",drivers);
if(drivers!=null)
url=prop.getProperty("url");
user=prop.getProperty("user");
password=prop.getProperty("password");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
driver.propertiesr 文件
的内容是
drivers=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433
DatabaseName=studentmanager
user=sa
password=sa