kan ikke få forbindelse mellem SQL og java
Jeg er ret i ny i java, og har en opgave i skolen hvor jeg skal få SQL til at arbejde sammen med java.Jeg har lavet en database i microsoft sql server 2000, og vil gerne kunne udskrive en tabel i en gui.
Jeg har skevet følgende:
import java.sql.*;
import java.awt.event.*;
import javax.sql.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class HotelApp extends javax.swing.JFrame {
/** Creates new form HotelApp */
public HotelApp() {
initComponents();
try {
// Connect to the Database
String driver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft:sqlserver://GERBER:1433;databasename=MandatoryHotel";
//
// String driver = "com.jnetdirect.jsql.JSQLDriver";
// String url = "jdbc:JSQLConnect://localhost/database=MandatoryHotel";
String userid = "GERBER";
String password = " ";
Class.forName( driver );
Connection conn = DriverManager.getConnection( url, userid, password );
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from Hotels");
while (rs.next()){
int hotelNo = rs.getInt(1);
String hotelName = rs.getString(2);
String address = rs.getString(3);
}
} catch(Exception e) {
System.out.println( e );
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setTitle("HotelApp af Allan Gerber");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jButton1.setText("Vis tabel");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, java.awt.BorderLayout.SOUTH);
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-410)/2, (screenSize.height-334)/2, 410, 334);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new HotelApp().show();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
Jeg tror ikke jeg kan få forbindelse til databasen da jeg får dette at vide:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
Håber nogen gider og kan hjælpe