如何建立JDBC連接和如何找到本地主機 請解決我的問題,並解釋我的代碼錯誤與解釋,我要集中精力,如何打印領域數據庫j中搖擺Java初學者在JDBC有些疑惑
package swingtesting;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* @author Admin
*/
public class NewClass {
public static void main(String[] args){
Connection con=null;
Statement st=null;
ResultSet rs=null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:3535;DatabaseName=swingmail;User=sa;Password=sa;");
st=con.createStatement();
String qry=" select * from empmail";
rs=st.executeQuery(qry);
System.out.println(rs.getString("empid"));
System.out.println(rs.getString("empname"));
System.out.println(rs.getString("desg"));
System.out.println(rs.getString("basic"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at swingtesting.NewJSwing.main(NewJSwing.java:130)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at swingtesting.NewJSwing.initComponents(NewJSwing.java:39)
at swingtesting.NewJSwing.<init>(NewJSwing.java:27)
at swingtesting.NewJSwing$2.run(NewJSwing.java:122)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
笏是這裏的問題?你沒有分享代碼錯誤? – Metalhead
這段代碼沒有使用Swing,所以它可能有助於清理你的問題到相關的元素。任何編譯時或運行時錯誤都會有所幫助。 –
我只是想知道如何在框架中的j swing上打印數據庫中的字段 – user1648224