当前位置: 技术问答>java相关
用Java如何连接SQL SERVRE(最好有源代码)
来源: 互联网 发布时间:2017-04-21
本文导语: 本人是一个Java的初学者(由vb转行),想了解Java如何连接数据库,请多多指教 | import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class JavaDb { private boolean packFrame =...
本人是一个Java的初学者(由vb转行),想了解Java如何连接数据库,请多多指教
|
import java.awt.*;
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;
}
}
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;
}
}
|
电击右下角的红色“管理”。
|
private static String dbDriver;//数据库驱动
private static String dbURL;//数据库路径
private Connection conn;//建立数据库连接对象
public static ResultSet rs;//结果集对象
conn = DriverManager.getConnection(dbURL,"jsp_user","jsp");
Statement stmt = conn.createStatement();
sql = "insert into leader (id,leader_name,leader_level,leader_position,leader_intro,department,working,tel,photo) values("+id+",'"+leader_name+"','"+leader_level+"','"+leader_position+"','"+leader_intro+"','"+department+"','"+working+"','"+tel+"','"+photo+"')";
success = stmt.executeUpdate(sql);
stmt.close();
conn.close();
private static String dbURL;//数据库路径
private Connection conn;//建立数据库连接对象
public static ResultSet rs;//结果集对象
conn = DriverManager.getConnection(dbURL,"jsp_user","jsp");
Statement stmt = conn.createStatement();
sql = "insert into leader (id,leader_name,leader_level,leader_position,leader_intro,department,working,tel,photo) values("+id+",'"+leader_name+"','"+leader_level+"','"+leader_position+"','"+leader_intro+"','"+department+"','"+working+"','"+tel+"','"+photo+"')";
success = stmt.executeUpdate(sql);
stmt.close();
conn.close();
|
有上角“管理”-〉“给分”
|
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://servername:1433","userid","password");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://servername:1433","userid","password");
|
JSP连接各种数据库的代码:
一、jsp连接Oracle8/8i/9i数据库(用thin模式)
testoracle.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
二、jsp连接Sql Server7.0/2000数据库
testsqlserver.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
三、jsp连接DB2数据库
testdb2.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
四、jsp连接Informix数据库
testinformix.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
五、jsp连接Sybase数据库
testmysql.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
六、jsp连接MySQL数据库
testmysql.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
七、jsp连接PostgreSQL数据库
testmysql.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
一、jsp连接Oracle8/8i/9i数据库(用thin模式)
testoracle.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
二、jsp连接Sql Server7.0/2000数据库
testsqlserver.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
三、jsp连接DB2数据库
testdb2.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
四、jsp连接Informix数据库
testinformix.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
五、jsp连接Sybase数据库
testmysql.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
六、jsp连接MySQL数据库
testmysql.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
七、jsp连接PostgreSQL数据库
testmysql.jsp如下:
您的第一个字段内容为:
您的第二个字段内容为:
|
1.先找个第四种驱动装上;当然走ODBC就不用说了;
2,了解那几个对象就OK了;
2,了解那几个对象就OK了;
|
Class.forName("com.inet.tds.TdsDriver");
try{
conn = DriverManager.getConnection("jdbc:inetdae:机器名称:1433? charset=gb2312","","");
try{
conn = DriverManager.getConnection("jdbc:inetdae:机器名称:1433? charset=gb2312","","");