我想連接JAVA到SQL SERVER,但我不知道該怎麼做,上個學期我們做了一個使用MS ACCESS的程序,因爲它是數據庫,我想知道如果我也可以使用它來將我創建的程序連接到SQL SERVER。將JAVA應用程序連接到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);}
}
我可以用這個?
您需要SQL Server的JDBC驅動程序。嘗試查看[this](http://msdn.microsoft.com/zh-cn/sqlserver/aa937724.aspx)初學者甚至[this](http://stackoverflow.com/questions/1310536/how -to-使-Java的工作與-SQL服務器) – MadProgrammer