我目前正在研究这个课程,我自己试图解决这个问题,但似乎我无法理解出了什么问题。
如何将其连接到我的数据库? :)
以下是我的代码:
package payroll.system;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JDialog;
import java.io.*;
import java.util.*;
import java.net.*;
public class clsConnection {
String url = "";
String username = "";
String password = "";
public Connection setConnection(Connection conn, String username, String password )
{
try
{
Properties props = new Properties();
String fileName = "MakeDB.ini"; FileInputStream in = new FileInputStream(fileName);
props.load(in);
String drivers = props.getProperty("jdbc.drivers");
if(drivers != null)
System.setProperty("jdbc.drivers", drivers);
url = props.getProperty("jdbc.url");
username = props.getProperty("jdbc.username");
password = props.getProperty("jdbc.password");
conn = DriverManager.getConnection(url,username,password);
}catch(SQLException e)
{
System.err.println("SQl Exception");
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("\nIO Exception");
}
catch (Exception e)
{
System.out.println("\nAnother Error");
}
return conn;
}
}
编译程序后,这是错误:
SQl Exception
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3080)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
答案 0 :(得分:1)
这是因为你没有设置odbc驱动程序。要设置odbc驱动程序,您需要转到Administrative Tools
下的Control Panel
并选择适当的驱动程序来设置ODBC。您可以访问MSDN了解详情。希望这会有所帮助。