我正在尝试将 JAVA 连接到 SQL SERVER,但我不知道该怎么做,上学期我们制作了一个使用 MS ACCESS 作为数据库的程序,我想知道我是否也可以使用它来连接我正在为 SQL SERVER 创建的程序。
这是我在 Java.Main 中使用的代码:
package pkg3a3pgroupsix;
import java.sql.*;
import javax.swing.*;
public class Main {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
Statement s;
public static Connection ConnectDatabase(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=BillingSystem.mdb");
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
public static void main(String[] args) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// TODO code application logic here
}
}
这是我用来登录的代码:
package pkg3a3pgroupsix;
import java.awt.*;
import java.nio.channels.SeekableByteChannel;
import javax.swing.*;
import java.sql.*;
public class Login extends javax.swing.JFrame {
Connection conn;
ResultSet rs = null;
PreparedStatement pst = null;
int counter = 0;
public void registration(){
User_Registration ur = new User_Registration();
ur.setVisible(true);
ur.setExtendedState(MAXIMIZED_BOTH);
this.dispose();
}
public void exit (){
int selectedOption = JOptionPane.showConfirmDialog(null,
"Do you wanna close the window?",
"Exit",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) { this.dispose();}
}
public Login() {
initComponents();
}
private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String sqllogin = "select * from Employee where Username='" + jTextField1.getText() + "' and Password='" + jPasswordField1.getText() + "'";
try {
pst = conn.prepareStatement(sqllogin);
rs = pst.executeQuery();
if (rs.next()) {
SelectionScreen s = new SelectionScreen();
s.setVisible(true);
this.dispose();counter = 0;}
else {JOptionPane.showMessageDialog(null, "Invalid Username or Password!");counter += 1;
if (counter == 1) {JOptionPane.showMessageDialog(null, "WARNING! 1st Attempt of Log-In!");}
else if (counter == 2) {JOptionPane.showMessageDialog(null, "WARNING! 2nd Attempt of Log-In!");}}
if (counter == 3) {JOptionPane.showMessageDialog(null, "WARNING! 3rd Attempt of Log-In!");
{JOptionPane.showMessageDialog(null, "The Program Will Shutdown.");this.dispose();}}}
catch (Exception e) {JOptionPane.showMessageDialog(null, e);}
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
conn = Main.ConnectDatabase();
}
private void btnexitActionPerformed(java.awt.event.ActionEvent evt) {
exit();
}
private void btnAdminActionPerformed(java.awt.event.ActionEvent evt) {
String sqllogin = "select * from Login where Username='" + jTextField1.getText() + "' and Password='" + jPasswordField1.getText() + "'";
try {
pst = conn.prepareStatement(sqllogin);
rs = pst.executeQuery();
if (rs.next()) {
User_Registration UR = new User_Registration();
UR.setVisible(true);
this.dispose();counter = 0;}
else {JOptionPane.showMessageDialog(null, "Invalid Username or Password!");counter += 1;
if (counter == 1) {JOptionPane.showMessageDialog(null, "WARNING! 1st Attempt of Log-In!");}
else if (counter == 2) {JOptionPane.showMessageDialog(null, "WARNING! 2nd Attempt of Log-In!");}}
if (counter == 3) {JOptionPane.showMessageDialog(null, "WARNING! 3rd Attempt of Log-In!");
{JOptionPane.showMessageDialog(null, "The Program Will Shutdown.");this.dispose();}}}
catch (Exception e) {JOptionPane.showMessageDialog(null, e);}
}
我可以用这个吗?