﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-陌上尘-文章分类-JAVA－J2ME</title><link>http://www.blogjava.net/moshangchen/category/32568.html</link><description>JVAA学习笔记</description><language>zh-cn</language><lastBuildDate>Wed, 25 Jun 2008 14:56:20 GMT</lastBuildDate><pubDate>Wed, 25 Jun 2008 14:56:20 GMT</pubDate><ttl>60</ttl><item><title>JAVA－SQL SERVER 数据库连接代码</title><link>http://www.blogjava.net/moshangchen/articles/210692.html</link><dc:creator>陌上尘</dc:creator><author>陌上尘</author><pubDate>Wed, 25 Jun 2008 14:25:00 GMT</pubDate><guid>http://www.blogjava.net/moshangchen/articles/210692.html</guid><wfw:comment>http://www.blogjava.net/moshangchen/comments/210692.html</wfw:comment><comments>http://www.blogjava.net/moshangchen/articles/210692.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/moshangchen/comments/commentRss/210692.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/moshangchen/services/trackbacks/210692.html</trackback:ping><description><![CDATA[import java.sql.*;
<br />
public class Test {
<br />
&nbsp;&nbsp;&nbsp;&nbsp;
<br />
&nbsp;&nbsp;
<br />
&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] args) {
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String sql;
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Connection conn;
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Statement stmt;&nbsp;&nbsp;&nbsp; //声明Statement对象
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ResultSet rs;&nbsp;&nbsp;&nbsp;
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;try
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=shopping";
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String user="sa";
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String password="sa";
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;conn= DriverManager.getConnection(url,user,password);
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;stmt=conn.createStatement();
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sql="select * from Users";
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;rs=stmt.executeQuery(sql);
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;while(rs.next())
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println(rs.getString(1));&nbsp;&nbsp; //输出数据库第一列
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println(rs.getString(2));
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;rs.close();
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;stmt.close();
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;conn.close();
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;catch(Exception e)
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;e.printStackTrace();
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; }
<br />
}
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ______________________________________________________________________________
<br />
&nbsp;<br />
&nbsp;<br />
import java.sql.*;
<br />
public class Test {
<br />
&nbsp;&nbsp;&nbsp;&nbsp;
<br />
&nbsp;&nbsp;
<br />
&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] args) {
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Connection conn;
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Statement stmt;
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;try
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=shopping";
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String user="sa";
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String password="sa";
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;conn= DriverManager.getConnection(url,user,password);
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;stmt=conn.createStatement();
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println(" 连接成功 !");&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;stmt.close();
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;conn.close();
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;catch(Exception e)
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;e.printStackTrace();
<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
<br />
&nbsp;&nbsp;&nbsp;&nbsp; }
<br />
}<br />
<img src ="http://www.blogjava.net/moshangchen/aggbug/210692.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/moshangchen/" target="_blank">陌上尘</a> 2008-06-25 22:25 <a href="http://www.blogjava.net/moshangchen/articles/210692.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>