package jsp;
import java.sql.*; // JDBC package
public class sql_data {
String url = "jdbc:inetdae:192.168.2.70?sql7=true"; // use your hostname and port number
String login = "sa"; // use your login here
String password =""; // use your password here
public Connection connection = null;
public Statement st = null;
public ResultSet rs = null;
public sql_data(){
try {
Class.forName("com.inet.tds.TdsDriver").newInstance();
DriverManager.setLoginTimeout(10);
} catch(Exception e) {
e.printStackTrace();
}
}public void sqlclose() {
try {
st.close();
connection.close();
}
catch(SQLException ex) {
System.err.println("sqlclose: " + ex.getMessage());
}
}public ResultSet executeQuery(String sql) {
try {
connection = DriverManager.getConnection(url,login,password);
connection.setCatalog( "register");
st = connection.createStatement();
rs = st.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}
import java.sql.*; // JDBC package
public class sql_data {
String url = "jdbc:inetdae:192.168.2.70?sql7=true"; // use your hostname and port number
String login = "sa"; // use your login here
String password =""; // use your password here
public Connection connection = null;
public Statement st = null;
public ResultSet rs = null;
public sql_data(){
try {
Class.forName("com.inet.tds.TdsDriver").newInstance();
DriverManager.setLoginTimeout(10);
} catch(Exception e) {
e.printStackTrace();
}
}public void sqlclose() {
try {
st.close();
connection.close();
}
catch(SQLException ex) {
System.err.println("sqlclose: " + ex.getMessage());
}
}public ResultSet executeQuery(String sql) {
try {
connection = DriverManager.getConnection(url,login,password);
connection.setCatalog( "register");
st = connection.createStatement();
rs = st.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}
还有这个
import java.io.*;
import java.util.*;
import java.sql.*;
import java.lang.*;public class DBConnect {
private String jdbcDriver=null;
private String jdbcURL=null;
private String userName=null;
private String password=null; public DBConnect() {
jdbcDriver="sun.jdbc.odbc.JdbcOdbcDriver";
jdbcURL="jdbc:odbc:shihong";
userName="system ";
password="manager";
}
public Connection getConnection(){
Connection connection=null;
try{
Class.forName(jdbcDriver);
connection=DriverManager.getConnection(jdbcURL,userName,password);
}catch(Exception e){
System.out.println(e);
}
return connection;
}
}
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class JavaDb
{
private boolean packFrame = false; public JavaDb()
{
Frame2 frame = new Frame2();
frame.validate();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
public static void main(String[] args)
{ new JavaDb();
}
}
class Frame2 extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JScrollPane jScrollPane1 = new JScrollPane();
private JTextArea jTextArea1 = new JTextArea();
private JButton jButton1 = new JButton(); //Construct the frame
public Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("Tested with SQL Server 7.0, Northwind database");
System.out.println("if it works, it is written by masterz, otherwise I don't know who write it");
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("How to connect SQL server database");
jTextArea1.setText("jTextArea1");
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.add(jScrollPane1, BorderLayout.CENTER);
contentPane.add(jButton1, BorderLayout.NORTH);
jScrollPane1.getViewport().add(jTextArea1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void processConn(Connection con,String fieldname) throws SQLException
{
Statement st = con.createStatement();
ResultSet res = st.executeQuery("select * from employees");
String line = "";
while (res.next())
line = line + res.getString(fieldname)+"\n";
jTextArea1.setText(line);
con.close();
}
void jButton1_actionPerformed(ActionEvent e2) {
try
{ processConn(getConn(),"notes");
}
catch(Exception e)
{
e.printStackTrace();
} }
Connection getConn() throws SQLException,ClassNotFoundException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DriverManager.setLoginTimeout(10);
Connection connection = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};Server=192.168.0.2;Database=Northwind","sa","sa");
// Connection connection = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};Server=cell;Database=Northwind","sa","sa");
connection.setCatalog( "northwind");
return connection;
}
}